leetcode-pp / 91alg-9-daily-check

5 stars 0 forks source link

【Day 54 】2022-12-24 - 746.使用最小花费爬楼梯 #61

Open azl397985856 opened 1 year ago

azl397985856 commented 1 year ago

746.使用最小花费爬楼梯

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/min-cost-climbing-stairs/

前置知识

每当你爬上一个阶梯你都要花费对应的体力值,一旦支付了相应的体力值,你就可以选择向上爬一个阶梯或者爬两个阶梯。

请你找出达到楼层顶部的最低花费。在开始时,你可以选择从下标为 0 或 1 的元素作为初始阶梯。

 

示例 1:

输入:cost = [10, 15, 20] 输出:15 解释:最低花费是从 cost[1] 开始,然后走两步即可到阶梯顶,一共花费 15 。  示例 2:

输入:cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] 输出:6 解释:最低花费方式是从 cost[0] 开始,逐个经过那些 1 ,跳过 cost[3] ,一共花费 6 。  

提示:

cost 的长度范围是 [2, 1000]。 cost[i] 将会是一个整型数据,范围为 [0, 999] 。

whoam-challenge commented 1 year ago

class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: n = len(cost) prev = curr = 0 for i in range(2, n + 1): nxt = min(curr + cost[i - 1], prev + cost[i - 2]) prev, curr = curr, nxt return curr

mayloveless commented 1 year ago

思路

动态规划爬楼梯,dp[i] = Math.min(dp[i-1] , dp[i-2])

代码

var minCostClimbingStairs = function(cost) {
    const n = cost.length;
    if (n<2) return cost[0];

    const dp = [];
    dp[0] = 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])
    }
    return dp[n];
};

复杂度

时间:O(n) 空间:O(n)

steven72574 commented 1 year ago

思路:动态规划

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n];//dp[n]代表爬到第n个台阶支付的最小费用
        dp[0] = 0;//爬到第0个台阶花费0
        dp[1] = 0;//爬到第一个台阶花费0
        for(int i = 2 ; i < n ; i++){
            dp[i] = Math.min( dp[i - 1] + cost[i - 1] , dp[i - 2] + cost[ i - 2]);
        }
        //爬到最后第一个台阶或最后第二个台阶,再详细对比
        return Math.min(dp[ n - 1 ] + cost[n - 1] , dp[n - 2] + cost[n - 2] );

    }
}

timeO(n) space O(n)

zjsuper commented 1 year ago

class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: lists = [0] * len(cost)

lists[i] means lowest cost till this step

    lists[0] = 0
    lists[1] = 0
    for i in range(2,len(cost)):
        lists[i] = min(lists[i-1]+cost[i-1],lists[i-2]+cost[i-2])

    return min(lists[-2]+cost[-2],lists[-1]+cost[-1])
heng518 commented 1 year ago

class Solution { public: int minCostClimbingStairs(vector& cost) { cost.push_back(0);

    for(int i = cost.size() - 3; i >= 0; i--)
        cost[i] += min(cost[i+1], cost[i+2]);

    return min(cost[0], cost[1]);
}

};

Alyenor commented 1 year ago
function minCostClimbingStairs(cost: number[]): number {
    const n=cost.length
    let 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])
    }
    return dp[n]
};
testplm commented 1 year ago
class Solution:
    def minCostClimbingStairs(self, cost):
        cur = 0 
        dp0 = cost[0]
        if len(cost) >= 2:
            dp1 = cost[1]

        for i in range(2, len(cost)):
            cur = cost[i] + min(dp0, dp1)
            dp0 = dp1
            dp1 = cur

        return min(dp0, dp1)
snmyj commented 1 year ago
var minCostClimbingStairs = function(cost) {
    cost.push(0)
    let arr = [cost[0],cost[1]];
    for(let i=2;i<cost.length;i++){
        arr[i]=Math.min(arr[i-1],arr[i-2])+cost[i];
    }
    return arr.pop();
};
buer1121 commented 1 year ago

class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: n = len(cost) dp = [0] * (n + 1) for i in range(2, n + 1): dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) return dp[n]

bookyue commented 1 year ago

code

    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n + 1];

        for (int i = 2; i <= n; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }

        return dp[n];
    }
FlipN9 commented 1 year ago
/**
    dp[i] 为了 reach 第 i 层 花的成本
*/ 

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int N = cost.length;
        int[] dp = new int[N + 1];
        for (int i = 2; i <= N; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[N];
    }
}

class Solution1 {
    public int minCostClimbingStairs(int[] cost) {
        int downOne = 0;
        int downTwo = 0;
        for (int i = 2; i < cost.length + 1; i++) {
            int temp = downOne;
            downOne = Math.min(downOne + cost[i - 1], downTwo + cost[i - 2]);
            downTwo = temp;
        }

        return downOne;
    }
}
chenming-cao commented 1 year ago

解题思路

DP。临界条件是minCosts[0] = 0, minCosts[1] = 0。状态转移方程是minCosts[i] = min(minCosts[i - 2] + cost[i - 2], minCosts[i - 1] + cost[i - 1]),即上到Step i的花费为从Step(i - 2)上两步的花费和从Step(i - 1)上一步的花费的最小值。

代码

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        // Use an array of size n + 1 to store the minimum costs
        int[] minCosts = new int[n + 1];
        // Base cases: we can either start from index 0 or index 1, no costs for these
        minCosts[0] = 0;
        minCosts[1] = 0;
        for (int i = 2; i < n + 1; i++) {
            // minimum cost at step i is the smaller one of the cost climing 2 steps from step (i - 2) and the cost climing 1 step from step (i - 1)
            minCosts[i] = Math.min(minCosts[i - 2] + cost[i - 2], minCosts[i - 1] + cost[i - 1]);
        }
        return minCosts[n];
    }
}

复杂度分析

zywang0 commented 1 year ago

class Solution { public int minCostClimbingStairs(int[] cost) { int n = cost.length; int[] dp = new int[n];//dp[n]代表爬到第n个台阶支付的最小费用 dp[0] = 0;//爬到第0个台阶花费0 dp[1] = 0;//爬到第一个台阶花费0 for(int i = 2 ; i < n ; i++){ dp[i] = Math.min( dp[i - 1] + cost[i - 1] , dp[i - 2] + cost[ i - 2]); } //爬到最后第一个台阶或最后第二个台阶,再详细对比 return Math.min(dp[ n - 1 ] + cost[n - 1] , dp[n - 2] + cost[n - 2] ); } }

Elon-Lau commented 1 year ago

class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: n = len(cost)

dp数组

    dp = [0] * (n+1)
    # 动态转移方程,需要判断对迭代顺序
    for i in range(2, n+1):
        dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2])
    return dp[n]
tiandao043 commented 1 year ago

思路

dp

代码

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        vector<int> ans;
        ans.push_back(cost[0]);
        ans.push_back(cost[1]);
        for(int i=2;i<cost.size();i++){
            int t=min(ans[i-1],ans[i-2])+cost[i];ans.push_back(t);
        }
        return min(ans[cost.size()-1],ans[cost.size()-2]);
    }
};

O(N) O(N)

A-pricity commented 1 year ago

/**
 * @param {number[]} cost
 * @return {number}
 */

var minCostClimbingStairs = function(cost) {
    const dp = [];
    dp[0] = dp[1] = 0;
    //楼梯顶为cost.length
    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];
};```
RestlessBreeze commented 1 year ago

思路

动态规划

代码

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int n = cost.size();
        int prev = 0, curr = 0;
        for (int i = 2; i <= n; i++) {
            int next = min(curr + cost[i - 1], prev + cost[i - 2]);
            prev = curr;
            curr = next;
        }
        return curr;
    }
};
klspta commented 1 year ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n + 1];
        dp[0] = cost[0];
        dp[1] = cost[1];
        for(int i = 2; i < n; i++){
            dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];
        }
        return Math.min(dp[n - 1], dp[n - 2]);
    }
}
BruceZhang-utf-8 commented 1 year ago

代码

Java Code:


class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n+1];
        dp[0] = dp[1] = 0;
        for(int i = 2;i <= n;i++){
            dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        return dp[n];
    }
}
xuanaxuan commented 1 year ago

思路

爬楼梯变形题,

var minCostClimbingStairs = function(cost) {
  const n=cost.length
  if ([0,1].includes(n)) return 0;

  let a = 0;
  let b = 0;
  let temp;

  for (let i = 2; i <= n; i++) {
    temp = Math.min(a+cost[i-2],b+cost[i-1] )  ;
    a = b;
    b = temp;
  }

  return temp;
};

复杂度

时间:O(N)

AtaraxyAdong commented 1 year ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {

        if (cost == null || cost.length <= 1) {
            return 0;
        }
        int[] dp = new int[cost.length + 1];
        dp[0] = cost[0];
        dp[1] = cost[1];

        for (int i = 2; i <= cost.length; i++) {
            dp[i] = Math.min(dp[i - 1], dp[i - 2]) + (i == cost.length ? 0 : cost[i]);
        }
        return dp[cost.length];
    }
}
paopaohua commented 1 year ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n+1];
        dp[0] = dp[1] = 0;
        for(int i = 2;i <= n;i++){
            dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        return dp[n];
    }
}
Jetery commented 1 year ago

746. 使用最小花费爬楼梯

思路

动态规划, 每一步只取前两步的最小, 答案在最后两个结果中取最小可以忽略奇偶

代码 (cpp)

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int n = cost.size();
        vector<int> q(n);
        q[0] = cost[0], q[1] = cost[1];
        for (int i = 2; i < n; i++) {
            q[i] = min(q[i - 1], q[i - 2]) + cost[i];
        }
        return min(q[n - 1], q[n - 2]);
    }
};

复杂度分析

whoam-challenge commented 1 year ago

class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: n = len(cost) dp = [0] * (n + 1) for i in range(2, n + 1): dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]) return dp[n]

chiehw commented 1 year ago
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int size = cost.size();
        vector<int> spend(size+1, 0);
        int minCost;

        spend[0] = spend[1] = 0;        
        for(int index=2; index < size+1; index++){
            spend[index] = min(spend[index - 1] + cost[index - 1], spend[index - 2] + cost[index - 2]);
        }

        return spend[size];
    }
};
Elsa-zhang commented 1 year ago
class Solution:
    def minCostClimbingStairs(self, cost: list[int]):
        cost = cost + [0]
        dp = [0]*len(cost)
        dp[0], dp[1] = cost[0], cost[1]
        for i in range(2, len(cost)):
            dp[i] = min(dp[i-1], dp[i-2]) + (cost[i])
        return dp[-1]