首页 > 其他 > 详细

LeetCode题目:Best Time to Buy and Sell Stock

时间:2016-03-07 10:28:31      阅读:170      评论:0      收藏:0      [点我收藏+]

原题地址: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;
    }
};

 

LeetCode题目:Best Time to Buy and Sell Stock

原文:http://www.cnblogs.com/runnyu/p/5249484.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!