forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 39
/
jumpGame.II.cpp
90 lines (77 loc) · 2.08 KB
/
jumpGame.II.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Source : https://oj.leetcode.com/problems/jump-game-ii/
// Author : Hao Chen
// Date : 2014-07-18
/**********************************************************************************
*
* 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.
*
* Your goal is to reach the last index in the minimum number of jumps.
*
* For example:
* Given array A = [2,3,1,1,4]
*
* The minimum number of jumps to reach the last index is 2.
* (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
*
*
**********************************************************************************/
#include <iostream>
using namespace std;
//Acutally, using the Greedy algorithm can have the answer
int jump(int A[], int n) {
if (n<=1) return 0;
int steps = 0;
int coverPos = 0;
for (int i=0; i<=coverPos&& i<n; ){
if (A[i]==0) return -1;
if(coverPos < A[i]+i){
coverPos = A[i]+i;
steps++;
}
if (coverPos >= n-1){
return steps;
}
//Greedy: find the next place which can have biggest distance
int nextPos=0;
int maxDistance=0;
for(int j=i+1; j<=coverPos; j++){
if ( A[j]+j > maxDistance ) {
maxDistance = A[j]+j;
nextPos = j;
}
}
i = nextPos;
}
return steps;
}
void printArray(int a[], int n){
cout << "{ ";
for(int i=0; i<n; i++){
if(i) cout << ", ";
cout << a[i];
}
cout << " } ";
}
int main()
{
#define TEST(a) printArray(a, sizeof(a)/sizeof(a[0])); cout<<jump(a, sizeof(a)/sizeof(a[0]))<<endl;
int a1[]={0};
TEST(a1);
int a2[]={1};
TEST(a2);
int a3[]={3,2,1,0,4};
TEST(a3);
int a4[]={2,3,1,1,4};
TEST(a4);
int a5[]={1,2,3};
TEST(a5);
// 0 -> 1 -> 4 -> end
int a6[]={2,3,1,1,4,0,1};
TEST(a6);
// 0 -> 1 -> 3 -> end
int a7[]={2,3,1,2,0,1};
TEST(a7);
return 0;
}