码迷,mamicode.com
首页 > 其他好文 > 详细

Best Time to Buy and Sell Stock系列

时间:2017-10-29 17:31:51      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:bsp   logs   size   time   toc   --   res   tor   nbsp   

// Best Time to Buy and Sell Stock 1
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size()==0) return 0; int maxres=prices[prices.size()-1]; int ans=0; for(int i=prices.size()-1;i>=0;i--) { maxres=max(maxres,prices[i]); ans=max(ans,maxres-prices[i]); } return ans; } };

  

// Best Time to Buy and Sell Stock 2
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        res=0;
        for(int i=0;i<n-1;i++)
       {
        if(prices[i]<prices[i+1])
        res+=prices[i+1]-prices[i];
       }
       return res;

};

  

Best Time to Buy and Sell Stock系列

标签:bsp   logs   size   time   toc   --   res   tor   nbsp   

原文地址:http://www.cnblogs.com/xlqtlhx/p/7750421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!