Problem: 122. 买卖股票的最佳时机 II
复杂度
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
Code
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int ans = 0;
for(int i = 1; i < n; i++)
{
if(prices[i] > prices[i-1])
ans += prices[i] - prices[i-1];
}
return ans;
}
}