-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump_game.cpp
57 lines (49 loc) · 1.18 KB
/
jump_game.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// https://leetcode.com/problems/jump-game/
// July 01, 2016
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums)
{
int n = nums.size();
int max_index = 0;
int i = 0;
bool can_jump = true;
while ( (i < nums.size()) && (max_index < (n-1)) )
{
if (max_index < i)
{
// we won't be able to jump i'th and onwards index
can_jump = false;
break;
}
max_index = max(max_index, min(i+nums[i], n-1));
++i;
}
return can_jump;
}
};
int main(int argc, char* argv[])
{
vector<int> nums = {3,2,1,0,4};
Solution sln;
bool can_jump = sln.canJump(nums);
if (can_jump)
{
cout << "can jump" << endl;
}
else
{
cout << "cannot jump" << endl;
}
return 0;
}
/**
* Approach: Greedy Time Complexity: O(n) Space Complexity: O(1)
* My solution is similar to: https://leetcode.com/discuss/15567/linear-and-simple-solution-in-c
*
* TBD: Understand the various approaches
* https://leetcode.com/articles/jump-game/
*/