Skip to content

Commit 6ec7f49

Browse files
committed
二刷746
1 parent 7579ea7 commit 6ec7f49

File tree

3 files changed

+65
-37
lines changed

3 files changed

+65
-37
lines changed

docs/0746-min-cost-climbing-stairs.adoc

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
11
[#0746-min-cost-climbing-stairs]
2-
= 746. Min Cost Climbing Stairs
2+
= 746. 使用最小花费爬楼梯
33

4-
{leetcode}/problems/min-cost-climbing-stairs/[LeetCode - Min Cost Climbing Stairs^]
4+
https://leetcode.cn/problems/min-cost-climbing-stairs/[LeetCode - 746. 使用最小花费爬楼梯 ^]
55

6+
给你一个整数数组 `cost` ,其中 `cost[i]` 是从楼梯第 `i` 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。
67

7-
On a staircase, the `i`-th step has some non-negative cost `cost[i]` assigned (0 indexed).
8+
你可以选择从下标为 `0` 或下标为 `1` 的台阶开始爬楼梯。
89

9-
Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.
10+
请你计算并返回达到楼梯顶部的最低花费。
1011

12+
*示例 1:*
1113

12-
*Example 1:*
14+
....
15+
输入:cost = [10,15,20]
16+
输出:15
17+
解释:你将从下标为 1 的台阶开始。
18+
- 支付 15 ,向上爬两个台阶,到达楼梯顶部。
19+
总花费为 15 。
20+
....
1321

22+
*示例 2:*
1423

15-
[subs="verbatim,quotes,macros"]
16-
----
17-
*Input:* cost = [10, 15, 20]
18-
*Output:* 15
19-
*Explanation:* Cheapest is start on cost[1], pay that cost and go to the top.
20-
----
21-
22-
23-
*Example 2:*
24-
25-
26-
[subs="verbatim,quotes,macros"]
27-
----
28-
*Input:* cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
29-
*Output:* 6
30-
*Explanation:* Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
31-
----
32-
24+
....
25+
输入:cost = [1,100,1,1,1,100,1,1,100,1]
26+
输出:6
27+
解释:你将从下标为 0 的台阶开始。
28+
- 支付 1 ,向上爬两个台阶,到达下标为 2 的台阶。
29+
- 支付 1 ,向上爬两个台阶,到达下标为 4 的台阶。
30+
- 支付 1 ,向上爬两个台阶,到达下标为 6 的台阶。
31+
- 支付 1 ,向上爬一个台阶,到达下标为 7 的台阶。
32+
- 支付 1 ,向上爬两个台阶,到达下标为 9 的台阶。
33+
- 支付 1 ,向上爬一个台阶,到达楼梯顶部。
34+
总花费为 6 。
35+
....
3336

34-
*Note:*
35-
36-
. `cost` will have a length in the range `[2, 1000]`.
37-
. Every `cost[i]` will be an integer in the range `[0, 999]`.
37+
*提示:*
3838

39+
* `+2 <= cost.length <= 1000+`
40+
* `+0 <= cost[i] <= 999+`
3941
4042
4143
== 思路分析
4244

4345
image::images/0746-01.jpg[{image_attr}]
4446

45-
46-
[[src-0736]]
47+
[[src-0746]]
4748
[tabs]
4849
====
4950
一刷::
@@ -55,14 +56,14 @@ include::{sourcedir}/_0746_MinCostClimbingStairs.java[tag=answer]
5556
----
5657
--
5758
58-
// 二刷::
59-
// +
60-
// --
61-
// [{java_src_attr}]
62-
// ----
63-
// include::{sourcedir}/_0078_Subsets_2.java[tag=answer]
64-
// ----
65-
// --
59+
二刷::
60+
+
61+
--
62+
[{java_src_attr}]
63+
----
64+
include::{sourcedir}/_0746_MinCostClimbingStairs_2.java[tag=answer]
65+
----
66+
--
6667
====
6768

6869
== 参考资料

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ endif::[]
270270
|{doc_base_url}/1137-n-th-tribonacci-number.adoc[题解]
271271
|✅ 动态规划
272272

273+
|{counter:codes2503}
274+
|{leetcode_base_url}/min-cost-climbing-stairs/[746. 使用最小花费爬楼梯^]
275+
|{doc_base_url}/0746-min-cost-climbing-stairs.adoc[题解]
276+
|✅ 动态规划
277+
273278

274279
|===
275280
˚
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0746_MinCostClimbingStairs_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-16 19:56:08
8+
*/
9+
public int minCostClimbingStairs(int[] cost) {
10+
int a = 0, b = 0, result = 0;
11+
for (int i = 2; i <= cost.length; i++) {
12+
// 第 i 阶,有两种走法
13+
// 1. 从第 i-1 阶走一步上去,花费: dp[i-1] 走到 i-1 的费用 + cost[i-1](向上的费用)
14+
// 2. 从第 i-2 阶走两步上去,花肥: dp[i-2] 走到 i-2 的费用 + cost[i-2](向上的费用)
15+
result = Math.min(a + cost[i - 2], b + cost[i - 1]);
16+
a = b;
17+
b = result;
18+
}
19+
return result;
20+
}
21+
// end::answer[]
22+
}

0 commit comments

Comments
 (0)