-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.cpp
More file actions
42 lines (37 loc) · 877 Bytes
/
JumpGame.cpp
File metadata and controls
42 lines (37 loc) · 877 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
41
42
#include<iostream>
#include<vector>
using namespace std;
// todo: 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.
bool canJump(vector<int>& nums) {
int count;
int p = nums.size();
bool flag = 1;
while(p>0)
{
p--;
count = 1;
if(nums[p] == 0 && p < nums.size()-1)
{
p--;
flag=0;
while(p>=0 && nums[p]<=count)
{
count++;
p--;
}
}
if(p==-1)
break;
flag=1;
}
return flag;
}
int main()
{
vector<int> v = {3,2,1,0,4};
cout << canJump(v);
}
// LC: Q.55