Leetcode 122. Best Time to Buy a
2018-10-30 本文已影响1人
SnailTyan
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
![](https://img.haomeiwen.com/i3232548/2fac282b38cf8eaf.png)
2. Solution
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() == 0) {
return 0;
}
int profit = 0;
for(int i = 1; i < prices.size(); i++) {
int diff = prices[i] - prices[i - 1];
if( diff > 0) {
profit += diff;
}
}
return profit;
}
};