[LeetCode] Jump Game VII

1871. Jump Game VII

You are given a 0-indexed binary string s and two integers minJump and maxJump. In the beginning, you are standing at index 0, which is equal to ‘0’. You can move from index i to index j if the following conditions are fulfilled:

  • i + minJump <= j <= min(i + maxJump, s.length - 1), and
  • s[j] == ‘0’.

Return true if you can reach index s.length - 1 in s, or false otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
bool canReach(string s, int minJump, int maxJump) {
if(s.back() == '1') return false;
vector<bool> v(s.length(), false);
set<int> g{0};
v[0] = true;
for(int i = 0; i < s.length(); i++) {
if(s[i] == '1' || !v[i]) continue;
for(int j = max(*prev(end(g)) + 1, i + minJump); j <= min((int)s.length() - 1, i + maxJump); j++) {
v[j] = true;
g.insert(j);
}
}

return v.back();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/23/PS/LeetCode/jump-game-vii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.