-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJump Game.cpp
More file actions
36 lines (26 loc) · 778 Bytes
/
Jump Game.cpp
File metadata and controls
36 lines (26 loc) · 778 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
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
if(n==1)
return true;
int i=n-2, j=n-1;
while(j>=0) {
if(nums[i] >= j-i) {
// A ok
j = i;
i = j-1;
} else {
i--;
}
if(j==0)
break;
if(i<0)
return false;
}
return true;
}
};
LEETCODE 55
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.