首页 > 其他 > 详细

【leetcode】122.Best Time to Buy and Sell Stock II

时间:2015-07-19 21:41:13      阅读:137      评论:0      收藏:0      [点我收藏+]
@requires_authorization
@author johnsondu
@create_time 2015.7.19 21:01
@url [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)
/************************
 * @description: dynamic programming.
 *    相邻元素做差,求出所有非负元素的和
 * @time_complexity:  O(n)
 * @space_complexity: O(1)
 ************************/
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int p_size = prices.size();
        if(p_size < 2) return 0;
        int ans = 0;
        for(int i = 1; i < p_size; i ++) {
            int tmp = prices[i] - prices[i-1];
            if(tmp > 0) ans += tmp;
        }
        return ans;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

【leetcode】122.Best Time to Buy and Sell Stock II

原文:http://blog.csdn.net/zone_programming/article/details/46958519

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