Skip to content

Commit 071033c

Browse files
committed
LeetCode 45. Jump Game II Rust
1 parent a2f2fdc commit 071033c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
5252
| [41. First Missing Positive][lc41] | 🔴 Hard | [![python](res/py.png)][lc41py] |
5353
| [42. Trapping Rain Water][lc42] | 🔴 Hard | [![python](res/py.png)][lc42py] |
5454
| [43. Multiply Strings][lc43] | 🟠 Medium | [![python](res/py.png)][lc43py] |
55-
| [45. Jump Game II][lc45] | 🟠 Medium | [![python](res/py.png)][lc45py] |
55+
| [45. Jump Game II][lc45] | 🟠 Medium | [![python](res/py.png)][lc45py] [![rust](res/rs.png)][lc45rs] |
5656
| [46. Permutations][lc46] | 🟠 Medium | [![python](res/py.png)][lc46py] |
5757
| [48. Rotate Image][lc48] | 🟠 Medium | [![python](res/py.png)][lc48py] |
5858
| [49. Group Anagrams][lc49] | 🟠 Medium | [![python](res/py.png)][lc49py] |
@@ -489,6 +489,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
489489
[lc43py]: leetcode/multiply-strings.py
490490
[lc45]: https://leetcode.com/problems/jump-game-ii/
491491
[lc45py]: leetcode/jump-game-ii.py
492+
[lc45rs]: leetcode/jump-game-ii.rs
492493
[lc46]: https://leetcode.com/problems/permutations/
493494
[lc46py]: leetcode/permutations.py
494495
[lc48]: https://leetcode.com/problems/rotate-image/

leetcode/jump-game-ii.py

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def test():
177177
Greedy,
178178
]
179179
tests = [
180+
[[0], 0],
180181
[[2, 3, 1, 1, 4], 2],
181182
[[2, 3, 0, 1, 4], 2],
182183
[[2, 3, 0, 1, 4, 0, 0, 0, 2, 8, 7, 3], 5],

leetcode/jump-game-ii.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 45. Jump Game II
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/jump-game-ii/
5+
//
6+
// Tags: Array - Dynamic Programming - Greedy
7+
8+
struct Solution;
9+
impl Solution {
10+
// Visit each element of the array but do it in groups, we can see it as
11+
// treating each group as a level and the algorithm as BFS. For each
12+
// level, keep track of the farthest position we could jump to from this
13+
// level. When we get to the end of the level, add one to the number of
14+
// jumps that we have taken, and update the current level by updating the
15+
// last element we can explore to match the farthest element we can
16+
// reach from this level.
17+
// The algorithm repeatedly calculates the farthest point we can reach
18+
// from any of the positions that we can reach given the current number
19+
// of jumps, then "jump" once more and continue calculating. Each element
20+
// is only explored once.
21+
//
22+
// Time complexity: O(n) - Each element is visited once.
23+
// Space complexity: O(1) - Constant space.
24+
//
25+
// Runtime 2 ms Beats 80%
26+
// Memory 2.1 MB Beats 71.3%
27+
pub fn jump(nums: Vec<i32>) -> i32 {
28+
let n = nums.len();
29+
let mut jumps = 0;
30+
let mut reach = 0;
31+
let mut next_reach = 0;
32+
for i in 0..n - 1 {
33+
let current_jump = i + nums[i] as usize;
34+
if current_jump > next_reach {
35+
next_reach = current_jump;
36+
}
37+
if next_reach >= n - 1 {
38+
return 1 + jumps as i32;
39+
}
40+
if i == reach {
41+
jumps += 1;
42+
reach = next_reach;
43+
}
44+
}
45+
jumps as i32
46+
}
47+
}
48+
49+
// Tests.
50+
fn main() {
51+
assert_eq!(Solution::jump(vec![0]), 0);
52+
assert_eq!(Solution::jump(vec![2, 3, 1, 1, 4]), 2);
53+
assert_eq!(Solution::jump(vec![2, 3, 0, 1, 4]), 2);
54+
assert_eq!(Solution::jump(vec![2, 3, 0, 1, 4, 0, 0, 0, 2, 8, 7, 3]), 5);
55+
println!("All tests passed!")
56+
}

0 commit comments

Comments
 (0)