0
点赞
收藏
分享

微信扫一扫

动态规划第二天

Python百事通 2022-01-04 阅读 53

一爬楼梯
题目描述

class Solution {
public:
    int climbStairs(int n) {
        int a = 1 , b = 2;
        int c = 0;
        if(n==1) return a;
        else if(n==2) return b;
        for(int i=2;i<n;i++){
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
};

[1,100,1,1,1,100,1,1,100,1,5,10,44,87,910,154,10,23]

二 使用最小花费爬楼梯

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        vector<int>toallCost(cost.size()+1);
        for(int i=2;i<=cost.size();i++)
           toallCost[i] = (toallCost[i-1]+cost[i-1]>toallCost[i-2]+cost[i-2]?toallCost[i-2]+cost[i-2]:toallCost[i-1]+cost[i-1]);
        
        return toallCost[toallCost.size()-1];
    }
};
举报

相关推荐

RHCSA第二天

CSS第二天

html第二天

出差第二天

MySQL第二天

集合第二天

java第二天

DOM第二天

JavaSE 第二天

0 条评论