leetcode -- Best Time to Buy and Sell Stock II

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

 

posted on 2014-05-28 21:18  berkeleysong  阅读(136)  评论(0编辑  收藏  举报

导航