-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJump_Game.cpp
More file actions
62 lines (50 loc) · 1.11 KB
/
Jump_Game.cpp
File metadata and controls
62 lines (50 loc) · 1.11 KB
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
58
59
60
61
// Source : https://oj.leetcode.com/problems/jump-game/
// Author : zheng yi xiong
// Date : 2015-02-13
/**********************************************************************************
*
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
* Each element in the array represents your maximum jump length at that position.
* Determine if you are able to reach the last index.
* For example:
* A = [2,3,1,1,4], return true.
* A = [3,2,1,0,4], return false.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(int A[], int n) {
if (1 >= n)
{
return true;
}
int min_step = 1;
for (int i = n - 2; i > 0; --i)
{
if (min_step > A[i])
{
++min_step;
}
else
{
min_step = 1;
}
}
if (A[0] >= min_step)
{
return true;
}
return false;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int A[] = {2,3,1,1,4};
Solution so;
bool bCan = so.canJump(A, 5);
return 0;
}