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

91 天学算法第五期打卡
55 stars 14 forks source link

【Day 54 】2021-11-02 - 746.使用最小花费爬楼梯 #73

Open azl397985856 opened 3 years ago

azl397985856 commented 3 years 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] 。

ABOUTY commented 3 years ago
class Solution:
    """
    time: O(n)
    space: O(1)
    """
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        first, second = 0, 0
        for i in range(2, len(cost) + 1):
            first, second = second, min(first + cost[i - 2], second + cost[i - 1])
        return second
52HzEcho commented 3 years ago

思路:创建一个数组存储每个节点最小下标,

/**
 * @param {number[]} cost
 * @return {number}
 */
var minCostClimbingStairs = function (cost) {
    let len = cost.length
    var dp = new Array(len + 1)
    dp[0] = 0; dp[1] = 0;
    for (let i = 2; i <= len; i++) {
        dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2])
    }
    return dp[len]
};
V-Enzo commented 3 years ago

思路

  1. 到n这个点,该点的cost也要算上。想象直接到位置1,需要加上位置1的cost。
  2. 到n这个点的最短距离,由到n-2和n-1这两个点的最短距离确定。到这两个点的最短距离+cost[n]
    class Solution {
    public:
    int minCostClimbingStairs(vector<int>& cost) {
        int N = cost.size();
        vector<int> step(N, 0);
        step[0] = cost[0];
        step[1] = cost[1];
        for(int i=2;i<N;i++){
            step[i] = cost[i] + min(step[i-2], step[i-1]);
        }
        return min(step[N-1], step[N-2]);
    }
    };

    Complexity:

    Time:O(n) Space:O(n)

L-SUI commented 3 years ago

var minCostClimbingStairs = function(cost) { let n = cost.length; let pre = cost[0]; let next = cost[1]; for(let i = 2;i < n;i++){ let tmp = next; next = Math.min(pre,next)+cost[i]; pre = tmp; } return Math.min(pre,next); };

septasset commented 3 years ago

代码(Python)

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        n = len(cost)
        # if n == 2:
        #     return min(cost[0], cost[1])
        f = [cost[0], cost[1]]
        for i in range(2, n):
            f.append(min(f[i-1], f[i-2])+cost[i])
        return min(f[n-1], f[n-2])
mokrs commented 3 years ago

$dp[i] = min(dp[i-2]+cost[i-2], dp[i-1]+cost[i-1])$

初始值:$dp[0]=dp[1]=0$

int minCostClimbingStairs(vector<int>& cost) {
    int p1 = 0, p2 = 0;

    for (int i = 2; i <= cost.size(); ++i) {
        int t = min(p1 + cost[i - 2], p2 + cost[i - 1]);
        p1 = p2;
        p2 = t;
    }

    return p2;
}
BpointA commented 3 years ago

思路

动态规划

Python3代码

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

复杂度

时间:O(n)

空间:O(n)

ysy0707 commented 3 years ago

思路:动态规划+空间优化

class Solution {
    //空间优化
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int pre = cost[0], cur = cost[1];
        for(int i = 2; i < n; i++){
            int tmp = Math.min(pre, cur) + cost[i];
            pre = cur;
            cur = tmp;
        }
        return Math.min(pre, cur);
    }
}

时间复杂度:O(N) 空间复杂度:O(1)

xjlgod commented 3 years ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int len = cost.length;
        int a = cost[0], b = cost[1];
        int i = 2;
        while (i < len) {
            int temp = Math.min(a, b) + cost[i];
            a = b;
            b = temp;
            i++;
        }
        return Math.min(a, b);
    }
}
HouHao1998 commented 3 years ago

思想

动态规划

代码

public int minCostClimbingStairs(int[] cost) {
        int minCost0 = 0;
        int minCost1 = Math.min(cost[0], cost[1]);
        int minCost = 0;
        for (int i = 2; i < cost.length; i++) {
            minCost = Math.min(minCost1 + cost[i], minCost0 + cost[i - 1]);
            minCost0 = minCost1;
            minCost1 = minCost;
        }
        return minCost;
    }

复杂度

时间复杂度:O(N) 空间复杂度:O(1)

liuyangqiQAQ commented 3 years ago

每次状态转移只和前一次状态相关。故有一个变量记录前一个状态即可

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int val1 = cost[0];
        int val2 = cost[1];
        //当前楼梯最小步数
        for (int i = 2; i < cost.length; i++) {
            int temp = val2;
            val2 = Math.min(val1, val2) + cost[i];
            val1 = temp;
        }
        return Math.min(val1, val2);
    }
}
dongzegithub commented 3 years ago
    // 暴力递归
    public int minCostClimbingStairs1(int[] cost) {
        return minCost(cost.length, cost);
    }

    private int minCost(int i, int[] cost) {
        if (i == 0 || i == 1) {
            return 0;
        }
        return Math.min(minCost(i - 1, cost) + cost[i - 1], minCost(i - 2, cost) + cost[i - 2]);
    }

    // dp
    public int minCostClimbingStairs(int[] cost) {
        int[] dp = new int[cost.length + 1];
        for (int i = 2; i < cost.length + 1; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[cost.length];
    }
MissNanLan commented 3 years ago

代码

JavaScript Code:

/**
 * @param {number[]} cost
 * @return {number}
 */
var minCostClimbingStairs = function (cost) {
  var dp = new Array(cost.length + 1);
  (dp[0] = 0), (dp[1] = 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];
};

复杂度分析

令 n 为数组长度。

yj9676 commented 3 years 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];
    }
}
vincentLW commented 3 years ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int minimumCost[] = new int[cost.length + 1];

        for (int i = 2; i < minimumCost.length; i++) {
            int takeOneStep = minimumCost[i - 1] + cost[i - 1];
            int takeTwoSteps = minimumCost[i - 2] + cost[i - 2];
            minimumCost[i] = Math.min(takeOneStep, takeTwoSteps);
        }

        return minimumCost[minimumCost.length - 1];
    }
}
BadCoderChou commented 3 years ago

Java Code

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

时间&空间 时间 O(n) 空间 O(n)

Wu-zonglin commented 3 years ago
class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        if len(cost) == 2: return min(cost[1], cost[0])
        dp = [float('inf') for _ in range(len(cost))]
        dp[1] = min(cost[1], cost[0])
        dp[2] = min(cost[1], cost[0]+cost[2])
        for i in range(3,len(cost)):
                dp[i] = min((dp[i-1]+cost[i], dp[i-2]+cost[i-1]))
        return dp[len(cost)-1]
HWFrankFung commented 3 years ago

Codes

var minCostClimbingStairs = function(cost) {
    let 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];
};
joriscai commented 3 years ago

思路

代码

javascript

/*
 * @lc app=leetcode.cn id=746 lang=javascript
 *
 * [746] 使用最小花费爬楼梯
 */

// @lc code=start
/**
 * @param {number[]} cost
 * @return {number}
 */
var minCostClimbingStairs = function(cost) {
  const n = cost.length
  const dp = new Array(n + 1)
  // 初始化,从第0或者第1的元素开始,此时花费都为0
  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]
};
// @lc code=end

复杂度分析

guangsizhongbin commented 3 years ago

func minCostClimbingStairs(cost []int) int { n := len(cost) // dp[i] 表示达到下标i的最小花费 dp := make([]int, n+1)

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

// 最后的元素的大小就是它的值
return dp[n]

}

func min(a, b int) int { if a < b { return a } return b }

KennethAlgol commented 3 years ago

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int minCost0 = 0;
        int minCost1 = Math.min(cost[0], cost[1]);
        int minCost = 0;
        for (int i = 2; i < cost.length; i++) {
            minCost = Math.min(minCost1 + cost[i], minCost0 + cost[i - 1]);
            minCost0 = minCost1;
            minCost1 = minCost;
        }
        return minCost;
    }
};
Maschinist-LZY commented 3 years ago

思路

动态规划

关键点

代码

C++ Code:


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

复杂度分析

令 n 为数组长度。

wangyifan2018 commented 3 years 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
Lydia61 commented 3 years ago

使用最小花费爬楼梯

思路

动态规划,递推

代码

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

复杂度分析

jaysonss commented 3 years ago

思路

代码

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

        for(int i=2;i<=cost.length;i++){
            dp[i] = Math.min(dp[i-2]+cost[i-2],dp[i-1]+cost[i-1]);
        }
        return dp[cost.length];
    }
}
yibenxiao commented 3 years ago

【Day 54】746.使用最小花费爬楼梯

思路

ans=min(A+B)+cost[i]

代码

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

复杂度

时间复杂度:O(N)

空间复杂度:O(1)

nonevsnull commented 3 years ago

思路

AC

代码

//从上往下想,top-down,递归
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        HashMap<Integer, Integer> map = new HashMap<>();
        return dp(cost, -1, map);
    }

    public int dp(int[] cost, int step, HashMap<Integer, Integer> map){
        if(step >= cost.length) return 0;
        if(map.containsKey(step)) return map.get(step);

        int res = (step < 0?0:cost[step]) + Math.min(dp(cost, step+1, map), dp(cost, step+2, map));
        map.put(step, res);
        return res;
    }
}

//从下往上想,bottom-up,递推
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int[] dp = new int[cost.length + 1];

        for(int i = 2;i < dp.length;i++){
            int oneStepToHere = dp[i - 1] + cost[i - 1];
            int twoStepToHere = dp[i - 2] + cost[i - 2];

            //here
            dp[i] = Math.min(oneStepToHere, twoStepToHere);
        }

        return dp[dp.length - 1];
    }
}

复杂度

time: O(N),遍历每个step space: O(N),都需要,无论是stack,map还是dp[]

qixuan-code commented 3 years 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]

复杂度:O(n)

winterdogdog commented 3 years ago
/**
 * @param {number[]} cost
 * @return {number}
 */
var minCostClimbingStairs = function(cost) {
    let len = cost.length;
    let dp = new Array(len);
    dp[0] = cost[0];
    dp[1] = cost[1];
    for(let i = 2; i < len; i++) {
        dp[i] = Math.min(dp[i - 2], dp[i - 1]) + cost[i]
    }
    return Math.min(dp[len - 1], dp[len - 2])
};
itsjacob commented 3 years ago

Intuition

Implementation

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

Complexity

machuangmr commented 3 years ago

题目746. 使用最小花费爬楼梯

思路

comst007 commented 3 years ago

746. 使用最小花费爬楼梯


思路

DP

f[ii] = min(f[ii - 1], f[ii - 2]) + cost[ii]

代码

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int n = cost.size();

        vector<int> f(n + 1, 0);
        int ii;
        f[1] = cost[0];
        f[2] = cost[1];
        for(ii = 3; ii <= n; ++ ii){
            f[ii] = cost[ii - 1] + min(f[ii - 1], f[ii - 2]);
        }

        return min(f[n], f[n - 1]);
    }
};

复杂度分析

n为nums长度。

Richard-LYF commented 3 years ago

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

erik7777777 commented 3 years ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int len = cost.length;
        for (int i = 2; i < cost.length; i++) {
            cost[i] += Math.min(cost[i - 1], cost[i - 2]);
        }
        return Math.min(cost[len - 1], cost[len - 2]);
    }
}
jinmenghan commented 3 years ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        if (n==2){
            return Math.min(cost[0],cost[1]);
        }
        int [] dp= new int[n];
        dp[0] = cost[0];
        dp[1] = cost[1];
        for(int i=2;i<n;i++){
            dp[i] = cost[i]+Math.min(dp[i-1], dp[i-2]);
        }
    // return Math.min(cost[n-1], cost[n-2]);
         return Math.min(dp[n-1], dp[n-2]);
    }
}
laurallalala commented 3 years ago

代码

class Solution(object):
    def minCostClimbingStairs(self, cost):
        """
        :type cost: List[int]
        :rtype: int
        """
        n = len(cost)
        dp = [float("inf")]*(n+1)
        cost.append(0)
        dp[0], dp[1] = cost[0], cost[1]
        for i in range(2, n+1):
            dp[i] = min(dp[i], dp[i-1]+cost[i], dp[i-2]+cost[i])
        return min(dp[n-1], dp[n])

复杂度

huzizailushang commented 3 years ago

class Solution { public: int minCostClimbingStairs(vector& cost) { int len = cost.size(); vector dp(len); //dp[i]: 爬过第i 级台阶所花费的体力值,注意这里是跨过而不是到达! dp[0] = cost[0]; //由题意易知,头两个元素的取值 dp[1] = cost[1]; for(int i = 2; i < len; i++) //这里len要相等 { dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]; //dp[i]:由dp[i-1]和dp[i -2]的较小者再加上当前位置的花费cost[i] } return min(dp[len - 1], dp[len - 2]); //注意跨过楼层顶部由前一步决定(可走一步也可走两步)。 } };

时间复杂度:O(n) 空间复杂度:O(n)

ymkymk commented 3 years ago

思路

体力值 10 15 20
水平线 0 1 2 天台 一次最多可以跨两步。cost[i]为离开当前台阶需要的体力,水平线出发不花体力。 如果离开水平线跨1步到了0级,耗费体力为0;然后离开0级跨一步或两步都是10体力,如果跨到2级,这时还没到天台,还要跨一步离开2级要20体力。所以共需要30体力。

代码

``


class Solution {

 public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] val = new int[n + 1];
        int prev = 0;
        int curr = 0;
        for (int i = 2; i <= n; i++) {
            int next = Math.min(prev + cost[i - 2], curr + cost[i - 1]);
            prev = curr;
            curr = next;
        }

        return curr;
    }
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(1)

flagyk5 commented 3 years ago
'''
时间 n
空间 n
'''

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        dp = [0]*(len(cost)+1)
        for i in range(len(dp)):
            if i == 0 or i == 1: dp[i] = cost[i]
            else: dp[i] = min(dp[i-1],dp[i-2]) + (0 if i == len(cost) else cost[i])
        return dp[-1]
falsity commented 3 years 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];
    }
}
JianXinyu commented 3 years ago

思路

skinnyh commented 3 years ago

Note

Solution 1

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        n = len(cost)
        # use cost for the dp array. dp[i] means the total cost to reach stair i
        for i in range(2, n):
            cost[i] += min(cost[i - 1], cost[i - 2])
        return min(cost[n - 1], cost[n - 2])

Time complexity: O(N)

Space complexity: O(1)

Solution 2

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        n = len(cost)
        # dp[i] means the total cost to reach the top from stair i
        dp = [0] * 2
        for i in range(n - 1, -1, -1):
            s1 = dp[(i + 1) % 2] if i + 1 < n else 0
            s2 = dp[(i + 2) % 2] if i + 2 < n else 0
            dp[i % 2] = cost[i] + min(s1, s2)
        return min(dp[0], dp[1])

Time complexity: O(N)

Space complexity: O(1)

Auto-SK commented 3 years ago

思路

动态规划,dp[i]=min(dp[i−1]+cost[i−1],dp[i−2]+cost[i−2])dp[0]=dp[1]=0

程序

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

复杂度

dahuang257 commented 3 years ago

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

SunStrongChina commented 3 years ago

746.使用最小花费爬楼梯

入选理由

暂无

题目地址

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

前置知识

  • 动态规划

题目描述

数组的每个下标作为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i](下标从 0 开始)。

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

请你找出达到楼层顶部的最低花费。在开始时,你可以选择从下标为 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] 。

朴素动态规划

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

时间复杂度:o(n) 空间复杂度:o(n)

Cartie-ZhouMo commented 3 years ago
class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        dp = [0] * (len(cost)+1)
        dp[0], dp[1] = cost[0], cost[1]
        for i in range(2, len(cost)+1):
            dp[i] = min(dp[i-1], dp[i-2]) + (cost[i] if i != len(cost) else 0)
        return dp[-1]
ozhfo commented 3 years ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n];
        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]);
    }
}
goddessIU commented 3 years ago

因为来的晚,所以跟不上现在节奏了,就自己随便发一个了

/**
 * @param {number} n
 * @param {number[][]} dislikes
 * @return {boolean}
 */
function deal(map, colors, color, i, n) {
    colors[i] = color;
    for(let j = 0;j<n;j++){
        if(map[i][j]==1) {
            if(colors[j]==color){
                return false;
            } else if(colors[j]==0&&!deal(map,colors,-1*color,j,n)){
                return false;
            }
        }
    }
    return true;
}
var possibleBipartition = function(n, dislikes) {
    const colors = new Array(n).fill(0);
    const map = new Array(n).fill([]).map(()=>{return new Array(n).fill(0)})
    for(let i = 0;i<dislikes.length;i++){
        map[dislikes[i][0]-1][dislikes[i][1]-1]=1;
        map[dislikes[i][1]-1][dislikes[i][0]-1]=1;
    }
    for(let i = 0;i<n;i++){
        if(colors[i]==0&&!deal(map,colors,1,i,n)){
            return false;
        }
    }
    return true;
};
LareinaWei commented 3 years ago

Thinking

DP. Use an array to track the total cost to the ith step.
Since we can choose to climb one or two steps at a time, dp[i] = min(dp[i-1], dp[i-2]) + cost[I].

Code

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

        if l == 0:
            return 0

        if l == 1:
            return cost[1]

        dp = [0 for _ in range(l)]

        dp[0] = cost[0]
        dp[1] = cost[1]

        for i in range(2, l):
            dp[i] = min(dp[i-1], dp[i-2]) + cost[i]

        return min(dp[l-1], dp[l-2])

Complexity

Time complexity: O(n).
Space complexity: O(n).

ZETAVI commented 3 years ago
class Solution {
    public int minCostClimbingStairs(int[] cost) {
        for (int i = 2; i < cost.length; i++) {
            cost[i] = Math.min(cost[i - 2], cost[i - 1]) + cost[i];
        }
        return Math.min(cost[cost.length - 2], cost[cost.length - 1]);
    }
}

复杂度分析

时间复杂度: O(n). 空间复杂度: O(n).