20240411 Finished
This commit is contained in:
parent
6c18bfa502
commit
2629985f53
|
@ -98,4 +98,5 @@ mod p80_remove_duplicates_from_sorted_array_ii;
|
|||
mod p169_majority_element;
|
||||
mod p189_rotate_array;
|
||||
mod p121_best_time_to_buy_and_sell_stock;
|
||||
mod p122_best_time_to_buy_and_sell_stock_ii;
|
||||
mod p122_best_time_to_buy_and_sell_stock_ii;
|
||||
mod p55_jump_game;
|
39
src/problem/p55_jump_game.rs
Normal file
39
src/problem/p55_jump_game.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* [55] Jump Game
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn can_jump(nums: Vec<i32>) -> bool {
|
||||
let mut max_reach = 0;
|
||||
|
||||
for i in 0..nums.len() {
|
||||
if i > max_reach {
|
||||
return false;
|
||||
}
|
||||
|
||||
max_reach = max_reach.max(i + nums[i] as usize);
|
||||
|
||||
if max_reach >= nums.len() - 1 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_55() {
|
||||
assert!(Solution::can_jump(vec![2, 3, 1, 1, 4]));
|
||||
assert!(!Solution::can_jump(vec![3, 2, 1, 0, 4]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user