-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3532.cpp
More file actions
25 lines (25 loc) · 787 Bytes
/
Copy path3532.cpp
File metadata and controls
25 lines (25 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
vector<bool> pathExistenceQueries(int n, vector<int>& nums, int maxDiff, vector<vector<int>>& queries) {
vector<int> groups(n, -1);
int index = 0;
int groupID = 0;
while (index < n) {
int start = index;
while (index + 1 < n && nums[index + 1] - nums[index] <= maxDiff) index++;
// [start, index];
for (int i = start; i <= index; ++i) {
groups[i] = groupID;
}
groupID++;
index++;
}
int m = queries.size();
vector<bool> res;
for (auto& q : queries) {
if (groups[q[0]] == groups[q[1]]) res.push_back(true);
else res.push_back(false);
}
return res;
}
};