Best Time to Buy and Sell Stock II LeetCode solution in C++

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

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0