您的位置:首页 > 其它

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

2015-07-19 21:23 190 查看
@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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: