leetcode -- Best Time to Buy and Sell Stock

class Solution {
public:
    int maxProfit(vector<int> &prices) {
       if(prices.size() == 0) return 0;
       vector<int> f1(prices.size());
       int minV = prices[0];
       f1[0] = 0;
       for(int i = 1;i<prices.size();i++)
       {
        minV = min(minV,prices[i]);
        f1[i] = max(f1[i-1],prices[i] - minV);///递推方程
       }
       
       return f1[prices.size() - 1];
        
    }
}; ///这是我第一次,用自己的动态规划思想,解决的第一道题目,哈哈哈

 

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

导航