[关闭]
@rg070836rg 2015-11-27T00:34:38.000000Z 字数 362 阅读 1135

Best Time to Buy and Sell Stock

leetcode 贪心


https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

  1. class Solution {
  2. public:
  3. int maxProfit(vector<int>& prices) {
  4. int n=prices.size();
  5. if(n<2) return 0;//?为什么在循环调不出来
  6. int cur_min=prices[0];//定义当前最小价格
  7. int max_profit=0;
  8. for(int i=1;i<n;i++)
  9. {
  10. max_profit=max(max_profit,prices[i]-cur_min);
  11. cur_min=min(cur_min,prices[i]);
  12. }
  13. return max_profit;
  14. }
  15. };
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注