Open azl397985856 opened 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
/**
* @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]
};
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]);
}
};
Time:O(n) Space:O(n)
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); };
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])
$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;
}
动态规划
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)
思路:动态规划+空间优化
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)
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);
}
}
动态规划
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)
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);
}
}
// 暴力递归
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];
}
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 为数组长度。
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];
}
}
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];
}
}
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)
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]
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];
};
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
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 }
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;
}
};
动态规划
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 为数组长度。
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
动态规划,递推
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]
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];
}
}
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)
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[]
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)
/**
* @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])
};
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];
}
};
class Solution {
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
for(int i = 2;i < n;i++) {
cost[i] = Math.min(cost[i - 2], cost[i - 1]) + cost[i];
}
return Math.min(cost[n - 2], cost[n - 1]);
}
}
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长度。
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]
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]);
}
}
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]);
}
}
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])
class Solution {
public:
int minCostClimbingStairs(vector
时间复杂度:O(n) 空间复杂度:O(n)
体力值 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)
'''
时间 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]
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];
}
}
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
vector<int> dp(cost.size()+1);
dp[0] = cost[0];
dp[1] = cost[1];
for(int i = 2; i <= cost.size(); ++i){
if(i < cost.size())
dp[i] = min(dp[i-1], dp[i-2]) + cost[i];
else
dp[i] = min(dp[i-1], dp[i-2]);
}
return dp.back();
}
};
T: O(n) S: O(n)
dp[i]
means the total cost to reach stair i. Then dp[i] = cost[i] + min(dp[i-1], dp[i-2])
. Result is min(dp[-1], dp[-2])
dp[i]
means the total cost to reach top from stair i. Then dp[i] = cost[i] + min(dp[i+1], dp[i+2]
. Result is min(dp[0], dp[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)
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)
动态规划,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]
class Solution {
public:
int minCostClimbingStairs(vector
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)
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]
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]);
}
}
/**
* @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;
};
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].
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])
Time complexity: O(n).
Space complexity: O(n).
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).
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] 。