Skip to content

Commit ca8bd8d

Browse files
committed
feat(greedy): add 55_jump_game.rs
1 parent 5c51b9c commit ca8bd8d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,9 @@ so that you can use `just tf TEST` command to test.
166166
| Problem | Difficulty | Solution |
167167
| - | - | - |
168168
| 56. Merge Intervals | Medium | [56_merge_intervals.rs](./tests/56_merge_intervals.rs) |
169+
170+
### Greedy
171+
172+
| Problem | Difficulty | Solution |
173+
| - | - | - |
174+
| 55. Jump Game | Medium | [55_jump_game.rs](./tests/55_jump_game.rs) |

tests/55_jump_game.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 55. Jump Game
2+
// https://leetcode.com/problems/jump-game/description/
3+
// Topics: Greedy.
4+
// Difficulty: Medium.
5+
6+
#[test]
7+
fn test_55_jump_game() {}
8+
9+
#[derive(Debug)]
10+
pub struct Solution;
11+
12+
// ---------------------------------
13+
// copy to leetcode starts from here
14+
// ---------------------------------
15+
16+
impl Solution {
17+
pub fn can_jump(nums: Vec<i32>) -> bool {
18+
let mut max_reach: usize = 0;
19+
for (i, &item) in nums.iter().enumerate() {
20+
if i > max_reach {
21+
return false;
22+
}
23+
max_reach = max_reach.max(i + item as usize);
24+
if max_reach >= nums.len() - 1 {
25+
return true;
26+
}
27+
}
28+
false
29+
}
30+
}

0 commit comments

Comments
 (0)