Skip to content

Commit 414c7a6

Browse files
authored
Create JumpGame2.cpp
1 parent 4e59823 commit 414c7a6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//https://www.youtube.com/watch?v=hZkb_Dqu7YY
2+
class Solution {
3+
public:
4+
int jump(vector<int>& nums) {
5+
int n = nums.size();
6+
if(n < 2) return 0;
7+
int maxIndexReachable = nums[0];
8+
int ans = 1, lim = nums[0];
9+
10+
for(int i = 1; i < n; i++)
11+
{
12+
if(i > lim) {
13+
ans++;
14+
lim = maxIndexReachable;
15+
}
16+
maxIndexReachable = max(maxIndexReachable, i+nums[i]);
17+
}
18+
19+
return ans;
20+
}
21+
};

0 commit comments

Comments
 (0)