面试题63. 股票的最大利润

题目:

 

 

解答:

 

 

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) 
 4     {
 5         int cost = INT_MAX;
 6         int profit = 0;
 7         for (int price: prices)
 8         {
 9             cost = std::min(cost, price);
10             profit = std::max(profit, price - cost);
11         }
12 
13         return profit;
14 
15     }
16 };

 

posted @ 2020-05-09 20:00  梦醒潇湘  阅读(116)  评论(0)    收藏  举报