-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:yi-ge/rust-practice
- Loading branch information
Showing
6 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 使用最小花费爬楼梯 | ||
// https://leetcode.cn/problems/min-cost-climbing-stairs | ||
// INLINE ../../images/array/min_cost_climbing_stairs.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 { | ||
let mut dp = vec![0; cost.len() + 1]; | ||
for i in 2..=cost.len() { | ||
dp[i] = std::cmp::min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); | ||
} | ||
dp[cost.len()] as i32 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use rust_practice::array::min_cost_climbing_stairs::Solution; | ||
|
||
#[test] | ||
fn min_cost_climbing_stairs() { | ||
// 示例 1: | ||
// 输入:cost = [10,15,20] | ||
// 输出:15 | ||
// 解释:你将从下标为 1 的台阶开始。 | ||
// - 支付 15 ,向上爬两个台阶,到达楼梯顶部。 | ||
// 总花费为 15 。 | ||
let cost = vec![10, 15, 20]; | ||
assert_eq!(Solution::min_cost_climbing_stairs(cost), 15); | ||
|
||
// 示例 2: | ||
// 输入:cost = [1,100,1,1,1,100,1,1,100,1] | ||
// 输出:6 | ||
// 解释:你将从下标为 0 的台阶开始。 | ||
// - 支付 1 ,向上爬两个台阶,到达下标为 2 的台阶。 | ||
// - 支付 1 ,向上爬两个台阶,到达下标为 4 的台阶。 | ||
// - 支付 1 ,向上爬两个台阶,到达下标为 6 的台阶。 | ||
// - 支付 1 ,向上爬一个台阶,到达下标为 7 的台阶。 | ||
// - 支付 1 ,向上爬两个台阶,到达下标为 9 的台阶。 | ||
// - 支付 1 ,向上爬一个台阶,到达楼梯顶部。 | ||
// 总花费为 6 。 | ||
let cost = vec![1, 100, 1, 1, 1, 100, 1, 1, 100, 1]; | ||
assert_eq!(Solution::min_cost_climbing_stairs(cost), 6); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters