码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode -- Best Time to Buy and Sell Stock

时间:2014-05-30 02:54:28      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

bubuko.com,布布扣
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];
        
    }
}; ///这是我第一次,用自己的动态规划思想,解决的第一道题目,哈哈哈
bubuko.com,布布扣

 

leetcode -- Best Time to Buy and Sell Stock,布布扣,bubuko.com

leetcode -- Best Time to Buy and Sell Stock

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/berkeleysong/p/3757873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!