-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGameVII5765.cpp
More file actions
40 lines (40 loc) · 964 Bytes
/
JumpGameVII5765.cpp
File metadata and controls
40 lines (40 loc) · 964 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
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
bool canReach(string s, int a, int b) {
int n = s.size(), cnt0 = 0;
if(s[n-1] == '1'){
return false;
}
if(maxConsecutive(s, '1') < b){
return true;
}
queue<int> q;
q.push(0);
while(!q.empty()){
int cur = q.front();
q.pop();
if(cur == n - 1){
return true;
}
for(int i = cur + a; i <= min(cur + b, n - 1); i++){
if(s[i] == '0'){
q.push(i);
}
}
}
return false;
}
private:
int maxConsecutive(const string& s, char c) {
int len = 0, n = s.size(), prev = 0, i = 0;
while(i < n){
while(i < n && s[i] == c){
i++;
}
len = max(len, i - prev);
prev = i + 1;
i++;
}
return len;
}
};