-
Notifications
You must be signed in to change notification settings - Fork 2
/
45.cpp
153 lines (132 loc) · 3.87 KB
/
45.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Author : Accagain
// Date : 17/3/26
// Email : [email protected]
/***************************************************************************************
*
* 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.)
*
* Note:
* You can assume that you can always reach the last index.
*
* 做法:
* 动态规划
* dp[i]表示从第i个位置,跳到最后最少的步数
* 不必考虑每个位置走到最后的最少步数,只考虑第一就行了
*
* 时间复杂度:
* O(n^2) 会超时,用优先队列也不行
* O(n) 贪心算法
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#define INF 0x3fffffff
using namespace std;
class Solution {
public:
int jump(vector<int>& nums) {
int can_farest=0, can_step=0;
int ans = 0;
for(int i=0; i < nums.size() - 1; i++)
{
can_farest = max(can_farest, nums[i]+i);
if(i == can_step)
{
ans++;
can_step = can_farest; // 走一步能到达的最远距离
}
}
return ans;
}
/*
struct Info
{
int min_step, pos;
Info(){}
Info(int min_step, int pos): min_step(min_step), pos(pos) {}
bool operator < (const Info & a) const
{
if(a.min_step < min_step)
return true;
else if(a.min_step == min_step)
{
if(a.pos < pos)
return true;
return false;
}
return false;
}
};
int jump(vector<int>& nums) {
int dp[nums.size()+1];
dp[nums.size() - 1] = 0;
priority_queue<Info> myq;
myq.push(Info(0, nums.size()-1));
//myq.push(Info(-1, 10));
//printf("%d\n", myq.top().min_step);;
for(int i=nums.size() - 2 ; i>=0; i--) {
priority_queue<Info> now;
if(nums[i] < 10)
{
dp[i] = INF;
for(int j=i+1; (j<=i+nums[i]) && (j<=nums.size()); j++)
dp[i] = min(dp[j]+1, dp[i]);
myq.push(Info(dp[i], i));
}
else
{
while(!myq.empty())
{
Info tmp = myq.top();
if(tmp.pos <= i+nums[i])
{
dp[i] = tmp.min_step+1;
myq.push(Info(dp[i], i));
//printf("%d %d\n", i, dp[i]);
break;
}
myq.pop();
now.push(tmp);
}
if(!now.empty())
{
//printf(":::%d\n", i);
while(!myq.empty())
{
now.push(myq.top());
myq.pop();
}
myq = now;
}
//myq = now;
//printf(":::%d %d\n", i, myq.size())
//printf("%d %d\n", i, dp[i]);
}
}
return dp[0];
}
*/
};
int main() {
Solution *test = new Solution();
int data[] = {2, 3, 1, 1, 4};
vector<int> x(data, data + sizeof(data) / sizeof(data[0]));
printf("%d\n", test->jump(x));
return 0;
}
//
// Created by cms on 17/3/26.
//