weinan030416

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

dp

121. 买卖股票的最佳时机 - 力扣(LeetCode)

复制代码
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int max=0;
        for(int i=0;i<prices.size();i++)
            for(int j=i;j<prices.size();j++)
            if(prices[j]-prices[i]>max)
            max=prices[j]-prices[i];
        return max;
    }
};
复制代码

超时

复制代码
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int dp[100000];
        int pmin=10000;
        int ans=0;
        pmin=prices[0];
        for(int i=1;i<prices.size();i++)
        {
            if(prices[i]<pmin)
            pmin=prices[i];
            dp[i]=max(dp[i-1],prices[i]-pmin);
            if(dp[i]>ans)
            ans=dp[i];
        }
        return ans;
    }
};

作者:relaxed-matsumotowq3
链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/solution/dong-tai-gui-hua-by-relaxed-matsumotowq3-zjxq/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
复制代码

 

posted on   楠030416  阅读(23)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示