From 1f305e047be12dd533a301a451d1e6d9e5b2cd7b Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 12:20:18 +0800 Subject: [PATCH 1/6] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 28 ++++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index d7258d45..98a58c12 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -351,17 +351,29 @@ function minCostClimbingStairs(cost: number[]): number { ### Rust ```Rust -use std::cmp::min; impl Solution { pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { - let len = cost.len(); - let mut dp = vec![0; len]; - dp[0] = cost[0]; - dp[1] = cost[1]; - for i in 2..len { - dp[i] = min(dp[i-1], dp[i-2]) + cost[i]; + let mut dp = vec![0; cost.len() + 1]; + for i in 2..=cost.len() { + dp[i] = (dp[i - 1] + cost[i - 1]).min(dp[i - 2] + cost[i - 2]); } - min(dp[len-1], dp[len-2]) + dp[cost.len()] + } +} +``` + +不使用 dp 数组 + +```rust +impl Solution { + pub fn min_cost_climbing_stairs(cost: Vec) -> i32 { + let (mut dp_before, mut dp_after) = (0, 0); + for i in 2..=cost.len() { + let dpi = (dp_before + cost[i - 2]).min(dp_after + cost[i - 1]); + dp_before = dp_after; + dp_after = dpi; + } + dp_after } } ``` From e9c4d54f537a1c42f71b5651bfc4baa0cd1a51e8 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 12:28:51 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 37 +++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 98a58c12..3d014858 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -330,22 +330,27 @@ var minCostClimbingStairs = function(cost) { ```typescript function minCostClimbingStairs(cost: number[]): number { - /** - dp[i]: 走到第i阶需要花费的最少金钱 - dp[0]: 0; - dp[1]: 0; - ... - dp[i]: min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); - */ - const dp = []; - const length = cost.length; - dp[0] = 0; - dp[1] = 0; - for (let i = 2; i <= length; i++) { - dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); - } - return dp[length]; -}; + const dp = [0, 0] + for (let i = 2; i <= cost.length; i++) { + dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + } + return dp[cost.length] +} +``` + +不使用 dp 数组 + +```typescript +function minCostClimbingStairs(cost: number[]): number { + let dp_before = 0, + dp_after = 0 + for (let i = 2; i <= cost.length; i++) { + let dpi = Math.min(dp_before + cost[i - 2], dp_after + cost[i - 1]) + dp_before = dp_after + dp_after = dpi + } + return dp_after +} ``` ### Rust From 1ad26f69cdffc20bec0c95669e929dab0f7d3b2c Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 12:37:03 +0800 Subject: [PATCH 3/6] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 41 +++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 3d014858..fb5261f3 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -312,17 +312,30 @@ func min(a, b int) int { ``` -### Javascript +### JavaScript + ```Javascript var minCostClimbingStairs = function(cost) { - const n = cost.length; - const dp = new Array(n + 1); - dp[0] = dp[1] = 0; - for (let i = 2; i <= n; ++i) { - dp[i] = Math.min(dp[i -1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + const dp = [0, 0] + for (let i = 2; i <= cost.length; ++i) { + dp[i] = Math.min(dp[i -1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + } + return dp[cost.length] +}; +``` + +不使用 dp 数组 + +```JavaScript +var minCostClimbingStairs = function(cost) { + let dpBefore = 0, + dpAfter = 0 + for(let i = 2;i <= cost.length;i++){ + let dpi = Math.min(dpBefore + cost[i - 2],dpAfter + cost[i - 1]) + dpBefore = dpAfter + dpAfter = dpi } - - return dp[n] + return dpAfter }; ``` @@ -342,14 +355,14 @@ function minCostClimbingStairs(cost: number[]): number { ```typescript function minCostClimbingStairs(cost: number[]): number { - let dp_before = 0, - dp_after = 0 + let dpBefore = 0, + dpAfter = 0 for (let i = 2; i <= cost.length; i++) { - let dpi = Math.min(dp_before + cost[i - 2], dp_after + cost[i - 1]) - dp_before = dp_after - dp_after = dpi + let dpi = Math.min(dpBefore + cost[i - 2], dpAfter + cost[i - 1]) + dpBefore = dpAfter + dpAfter = dpi } - return dp_after + return dpAfter } ``` From ac16522a48d90d45d2f87f50a2c336fc383f4134 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 12:37:46 +0800 Subject: [PATCH 4/6] =?UTF-8?q?Update=20problems/0746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index fb5261f3..561441fc 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -314,7 +314,7 @@ func min(a, b int) int { ### JavaScript -```Javascript +```JavaScript var minCostClimbingStairs = function(cost) { const dp = [0, 0] for (let i = 2; i <= cost.length; ++i) { From 057d6b8f89ea5bbef1bb460ce40d9093abd1c3e0 Mon Sep 17 00:00:00 2001 From: fw_qaq Date: Thu, 13 Apr 2023 13:34:46 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Update=200746.=E4=BD=BF=E7=94=A8=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 33 +++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 561441fc..f11439c0 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -399,18 +399,29 @@ impl Solution { ### C ```c -int minCostClimbingStairs(int* cost, int costSize){ - //开辟dp数组,大小为costSize - int *dp = (int *)malloc(sizeof(int) * costSize); - //初始化dp[0] = cost[0], dp[1] = cost[1] - dp[0] = cost[0], dp[1] = cost[1]; +#include +int minCostClimbingStairs(int *cost, int costSize) { + int dp[costSize + 1]; + dp[0] = dp[1] = 0; + for (int i = 2; i <= costSize; i++) { + dp[i] = fmin(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]); + } + return dp[costSize]; +} +``` - int i; - for(i = 2; i < costSize; ++i) { - dp[i] = (dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]) + cost[i]; - } - //选出倒数2层楼梯中较小的 - return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]; +不使用 dp 数组 + +```c +#include +int minCostClimbingStairs(int *cost, int costSize) { + int dpBefore = 0, dpAfter = 0; + for (int i = 2; i <= costSize; i++) { + int dpi = fmin(dpBefore + cost[i - 2], dpAfter + cost[i - 1]); + dpBefore = dpAfter; + dpAfter = dpi; + } + return dpAfter; } ``` From 341b0aa672e9ca2d8a5b1cea2bc5ba307e795eda Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 17 Apr 2023 10:37:39 +0800 Subject: [PATCH 6/6] =?UTF-8?q?Update=20problems/0746.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index f11439c0..31e7bd48 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -318,7 +318,7 @@ func min(a, b int) int { var minCostClimbingStairs = function(cost) { const dp = [0, 0] for (let i = 2; i <= cost.length; ++i) { - dp[i] = Math.min(dp[i -1] + cost[i - 1], dp[i - 2] + cost[i - 2]) + dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) } return dp[cost.length] };