LeetCode题目:Best Time to Buy and Sell Stock

原题地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

解决方法:动态规划,minimun存储的是当前价格中最小的。

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int minimum = INT_MAX, ret = 0, size = prices.size();
        for(int i = 0; i < size; ++i){
            if(prices[i] < minimum)
                minimum = prices[i];
            int diff = prices[i] - minimum;
            if(diff > ret)
                ret = diff;
        }
        return ret;
    }
};

 

posted @ 2016-03-07 09:40  Runnyu  阅读(174)  评论(0编辑  收藏  举报