121-best-time-to-buy-and-sell-stock.h 364 字节
Newer Older
辉哈's avatar
辉哈 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int left = 0, right = 0;
        int max = 0, temp = 0;
        for(auto i = 0; i < prices.size(); ++i) {
            right = i;
            temp = prices[right] - prices[left];
            if(temp < 0) left = i;
            if(max < temp) max = temp;
        }
        return max;
    }
};