[LeetCode]Best Time to Buy and Sell Stock

来源:互联网 发布:php实现导出excel 编辑:程序博客网 时间:2024/05/17 08:59
解题思路:
记录两个状态
1,当前最大收益maxprofit = max(maxprofit, prices[i] - minCost);

2,以及当前最小花费 minCost = min ( minCost , prices[i] ) 


class Solution {public:    int maxProfit(vector<int>& prices) {        if (prices.size() == 0) return 0;        int maxProfit = 0;        int minCost = prices[0];        for(int i = 1; i < prices.size(); ++i){            maxProfit = max(prices[i] - minCost, maxProfit);            minCost = min(minCost, prices[i]);        }        return maxProfit;    }};


0 0