mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -312,17 +312,30 @@ func min(a, b int) int {
|
||||
```
|
||||
|
||||
|
||||
### 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])
|
||||
}
|
||||
### JavaScript
|
||||
|
||||
return dp[n]
|
||||
```JavaScript
|
||||
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])
|
||||
}
|
||||
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 dpAfter
|
||||
};
|
||||
```
|
||||
|
||||
@ -330,38 +343,55 @@ 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]);
|
||||
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[length];
|
||||
};
|
||||
return dp[cost.length]
|
||||
}
|
||||
```
|
||||
|
||||
不使用 dp 数组
|
||||
|
||||
```typescript
|
||||
function minCostClimbingStairs(cost: number[]): number {
|
||||
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 dpAfter
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
```Rust
|
||||
use std::cmp::min;
|
||||
impl Solution {
|
||||
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> 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>) -> 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
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -369,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];
|
||||
|
||||
int i;
|
||||
for(i = 2; i < costSize; ++i) {
|
||||
dp[i] = (dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]) + cost[i];
|
||||
#include <math.h>
|
||||
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]);
|
||||
}
|
||||
//选出倒数2层楼梯中较小的
|
||||
return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2];
|
||||
return dp[costSize];
|
||||
}
|
||||
```
|
||||
|
||||
不使用 dp 数组
|
||||
|
||||
```c
|
||||
#include <math.h>
|
||||
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;
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user