A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 45. Jump Game II, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming, greedy.
The notes already sitting in the source point us in the right direction:
- TLE
- 91 / 92 test cases passed.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are jump.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//91 / 92 test cases passed.
03class Solution {
04public:
05 int jump(vector<int>& nums) {
06 int n = nums.size();
07
08 vector<int> dp(n, INT_MAX);
09
10 dp[0] = 0;
11
12 for(int i = 0; i < n; ++i){
13 for(int j = i+1; j <= min(n-1, i+nums[i]); ++j){
14 //we can jump from i to j
15 dp[j] = min(dp[j], dp[i]+1);
16 }
17 }
18
19 return dp[n-1];
20 }
21};
22
23//monotonic deque
24//https://leetcode.com/problems/jump-game-ii/discuss/230778/C%2B%2B-deque-based-beats-99
25//Runtime: 20 ms, faster than 87.98% of C++ online submissions for Jump Game II.
26//Memory Usage: 13.6 MB, less than 18.24% of C++ online submissions for Jump Game II.
27class Solution {
28public:
29 int jump(vector<int>& nums) {
30 int n = nums.size();
31
32 if(n <= 1) return 0;
33
34 /*
35 pair: (steps required to jump to i, farthest position reachable from i)
36 later element requires more steps, but it can reach farther position
37 */
38 deque<pair<int, int>> dq;
39
40 dq.push_back({0, nums[0]});
41
42 for(int i = 1; i < n; ++i){
43 while(!dq.empty() && dq.front().second < i){
44 //cannot reach current position
45 dq.pop_front();
46 }
47
48 if(i+nums[i] > dq.back().second){
49 /*
50 if the farthest position(p) can be reached from i is
51 same as j(j < i),
52 then we can discard i,
53 that's because the steps required by j
54 to reach p is definitely <=
55 the steps required by i
56 */
57 dq.push_back({dq.front().first+1, i+nums[i]});
58 }
59 }
60
61 //dq.front() has minimum steps required to reach n-1
62 return dq.front().first+1;
63 }
64};
65
66//bfs
67//https://leetcode.com/problems/jump-game-ii/discuss/18019/10-lines-C%2B%2B-(16ms)-Python-BFS-Solutions-with-Explanations
68//Runtime: 32 ms, faster than 23.31% of C++ online submissions for Jump Game II.
69//Memory Usage: 13.2 MB, less than 88.44% of C++ online submissions for Jump Game II.
70class Solution {
71public:
72 int jump(vector<int>& nums) {
73 int n = nums.size();
74 int start = 0, end = 0, jump = 0;
75
76 //while we cannot reach n-1
77 while(end < n-1){
78 // cout << "jump: " << jump << ", [" << start << ", " << end << "]" << endl;
79 int nextend = end;
80
81 //calculate the farthest position we can jump from current range
82 for(int i = start; i <= min(end, n-1); ++i){
83 // cout << "i: " << i << ", " << i + nums[i] << endl;
84 nextend = max(nextend, i + nums[i]);
85 }
86
87 //assume we just jump one position from last end
88 start = end+1;
89 end = nextend;
90 ++jump;
91 // cout << "jump: " << jump << ", [" << start << ", " << end << "]" << endl;
92 }
93
94 return jump;
95 }
96};
97
98//Greedy
99//https://leetcode.com/problems/jump-game-ii/discuss/18014/Concise-O(n)-one-loop-JAVA-solution-based-on-Greedy
100//Runtime: 28 ms, faster than 36.18% of C++ online submissions for Jump Game II.
101//Memory Usage: 13.2 MB, less than 80.46% of C++ online submissions for Jump Game II.
102class Solution {
103public:
104 int jump(vector<int>& nums) {
105 int n = nums.size();
106
107 int jumps = 0;
108 int cur_end = 0, cur_max_reach = 0;
109
110 /*
111 i stops at n-2,
112 that's because we don't need to jump
113 if we have reached n-1
114 */
115 for(int i = 0; i < n-1; ++i){
116 cur_max_reach = max(cur_max_reach, i + nums[i]);
117
118 if(i == cur_end){
119 cur_end = cur_max_reach;
120 ++jumps;
121 }
122 }
123
124 return jumps;
125 }
126};
Cost