-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0045_jump.cc
55 lines (50 loc) · 1.03 KB
/
Problem_0045_jump.cc
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
#include <algorithm>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int jump(vector<int>& nums)
{
int ans = 0;
int n = nums.size();
int jump = 0;
// 当前能跳到的最远距离
int cur = 0;
// 下一步能跳到的最远距离
int next = 0;
for (int i = 0; i < n; i++)
{
// 来到i下标
// cur包括了i所在的位置,不用付出额外步数
// cur没有包括i所在的位置,需要付出额外步数
if (cur < i)
{
jump++;
cur = next;
}
next = std::max(next, i + nums[i]);
}
return jump;
}
};
void testJump()
{
Solution s;
vector<int> n1 = {2, 3, 1, 1, 4};
vector<int> n2 = {2, 3, 0, 1, 4};
vector<int> n3 = {1, 1, 1, 1, 1};
vector<int> n4 = {3, 2, 1, 4, 5, 1, 1, 1, 3};
EXPECT_EQ_INT(2, s.jump(n1));
EXPECT_EQ_INT(2, s.jump(n2));
EXPECT_EQ_INT(4, s.jump(n3));
EXPECT_EQ_INT(3, s.jump(n4));
EXPECT_SUMMARY;
}
int main()
{
testJump();
return 0;
}