-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1871.cpp
More file actions
30 lines (30 loc) · 940 Bytes
/
Copy path1871.cpp
File metadata and controls
30 lines (30 loc) · 940 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
26
27
28
29
30
class Solution {
public:
bool canReach(string s, int minJump, int maxJump) {
queue<int> q;
q.push(0);
int n = s.size();
vector<bool> visited(n, false);
visited[0] = true;
int maxIndex = 0;
while (!q.empty()) {
int m = q.size();
while (m--) {
int curr = q.front();
q.pop();
if (curr == n - 1) return true;
int last = min(curr + maxJump, n - 1);
int first = max(maxIndex, curr + minJump);
for (int index = first; index <= last; index++) {
if (visited[index]) continue;
if (s[index] == '0') {
q.push(index);
}
visited[index] = true;
maxIndex = max(maxIndex, index);
}
}
}
return false;
}
};