Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594

[LeetCode]Best Time to Buy and Sell Stock II-2014.01.20

Posted on 2014-01-20 21:51 Uriel 阅读(143) 评论(0)  编辑 收藏 引用 所属分类: LeetCode
跟Best Time to Buy and Sell Stock一样,而且可以进行N次买入/卖出操作,于是如果连续两天股票市值之差大于零就累加就行了~

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         int profit[100010], mx = 0, n = prices.size();
 5         for(int i = 0; i < n - 1; ++i) {
 6             profit[i + 1] = prices[i + 1] - prices[i];
 7         }
 8         for(int i = 1; i < n; ++i) {
 9             if(profit[i] > 0) mx += profit[i];
10         }
11         return mx;
12     }
13 };

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理