https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3287/
2.Idea
Just keep adding positive diffs.
3.Source
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i - 1] < prices[i]) {
ans += (prices[i]- prices[i - 1]);
}
}
return ans;
}
};
No comments:
Post a Comment