Merge branch 'master' of git.rrricardo.top:jackfiled/leetcode
This commit is contained in:
commit
b77e0295b2
|
@ -98,4 +98,7 @@ 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 p55_jump_game;
|
||||
mod p45_jump_game_ii;
|
||||
mod p274_h_index;
|
44
src/problem/p122_best_time_to_buy_and_sell_stock_ii.rs
Normal file
44
src/problem/p122_best_time_to_buy_and_sell_stock_ii.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
* [122] Best Time to Buy and Sell Stock II
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn max_profit(prices: Vec<i32>) -> i32 {
|
||||
let mut result = 0;
|
||||
let mut once_buy = 0;
|
||||
let mut min_buy = prices[0];
|
||||
|
||||
for i in 1..prices.len() {
|
||||
if prices[i] > prices[i - 1] {
|
||||
once_buy = once_buy.max(prices[i] - min_buy);
|
||||
} else if prices[i] < prices[i - 1] {
|
||||
result += once_buy;
|
||||
once_buy = 0;
|
||||
min_buy = prices[i];
|
||||
}
|
||||
}
|
||||
|
||||
if once_buy != 0 {
|
||||
result += once_buy;
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_122() {
|
||||
assert_eq!(7, Solution::max_profit(vec![7, 1, 5, 3, 6, 4]));
|
||||
assert_eq!(4, Solution::max_profit(vec![1, 2, 3, 4, 5]));
|
||||
assert_eq!(0, Solution::max_profit(vec![7, 6, 4, 3, 2, 1]));
|
||||
}
|
||||
}
|
41
src/problem/p45_jump_game_ii.rs
Normal file
41
src/problem/p45_jump_game_ii.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* [45] Jump Game II
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn jump(nums: Vec<i32>) -> i32 {
|
||||
let mut dp = vec![i32::MAX; nums.len()];
|
||||
dp[0] = 0;
|
||||
|
||||
for i in 0..nums.len() {
|
||||
if dp[i] == i32::MAX {
|
||||
continue;
|
||||
}
|
||||
|
||||
for j in 1..=nums[i] as usize {
|
||||
if i + j >= nums.len() {
|
||||
break;
|
||||
}
|
||||
|
||||
dp[i + j] = dp[i + j].min(dp[i] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
dp[nums.len() - 1]
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_45() {
|
||||
assert_eq!(2, Solution::jump(vec![2, 3, 1, 1, 4]));
|
||||
}
|
||||
}
|
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