首页 > 其他 > 详细

121. Best Time to Buy and Sell Stock

时间:2018-05-18 19:12:30      阅读:198      评论:0      收藏:0      [点我收藏+]
 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     int maxProfit(vector<int>& prices) 
12     {
13         int buy=INT_MAX;
14         int sell=0;
15         int sz=prices.size();
16         for(int i=0;i<sz;i++)
17         {
18             buy=min(buy,prices[i]);
19             sell=max(sell,prices[i]-buy);
20         }
21         return sell;
22     }
23 };

扫描一遍数组,用当前值减去之前的最小值,即为当前可获得的最大利润。扫描完毕后,即为买卖一次能获得的最大利润。

121. Best Time to Buy and Sell Stock

原文:https://www.cnblogs.com/zhuangbijingdeboke/p/9057447.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!