LeetCode: Best Time to Buy and Sell Stock II

LeetCode: Best Time to Buy and Sell Stock II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class BestTimeToBuyAndSellStockII {
public int maxProfit(int[] prices) {
if (prices.length < 2) {
return 0;
}
int result = 0;
for (int i = 1; i < prices.length; i++) {
int diff = prices[i] - prices[i - 1];
if (diff > 0) {
result += diff;
}
}
return result;
}
}