Skip to content

Commit

Permalink
add: 使用最小花费爬楼梯
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 17, 2023
1 parent 2488b59 commit 919ee92
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [使用最小花费爬楼梯](src/array/min_cost_climbing_stairs.rs) [数组, 动态规划]

- LeetCode 746. 使用最小花费爬楼梯 <https://leetcode.cn/problems/min-cost-climbing-stairs>

- [用邮票贴满网格图](src/array/stamping_the_grid.rs) [贪心, 数组, 矩阵, 前缀和]

- LeetCode 2132. 用邮票贴满网格图 <https://leetcode.cn/problems/stamping-the-grid>
Expand Down
Binary file added images/array/min_cost_climbing_stairs.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/array/min_cost_climbing_stairs.rs
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
}
}
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ pub mod number_of_unequal_triplets_in_array;
pub mod number_of_times_binary_string_is_prefix_aligned;
pub mod house_robber;
pub mod stamping_the_grid;
pub mod min_cost_climbing_stairs;
27 changes: 27 additions & 0 deletions tests/array/min_cost_climbing_stairs_test.rs
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);
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ pub mod number_of_unequal_triplets_in_array_test;
pub mod number_of_times_binary_string_is_prefix_aligned_test;
pub mod house_robber_test;
pub mod stamping_the_grid_test;
pub mod min_cost_climbing_stairs_test;

0 comments on commit 919ee92

Please sign in to comment.