-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathJump Game VII.java
30 lines (24 loc) · 1.05 KB
/
Jump Game VII.java
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 boolean canReach(String s, int minJump, int maxJump) {
if(s.charAt(s.length() - 1) != '0')
return false;
Queue<Integer> queue = new LinkedList<>();
queue.add(0);
// This variable tells us till which index we have processed
int maxReach = 0;
while(!queue.isEmpty()){
int idx = queue.remove();
// If we reached the last index
if(idx == s.length() - 1)
return true;
// start the loop from max of [current maximum (idx + minJump), maximum processed index (maxReach)]
for(int j = Math.max(idx + minJump, maxReach); j <= Math.min(idx + maxJump, s.length() - 1); j++){
if(s.charAt(j) == '0')
queue.add(j);
}
// since we have processed till idx + maxJump so update maxReach to next index
maxReach = Math.min(idx + maxJump + 1, s.length() - 1);
}
return false;
}
}