https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if (n == 1) {
return 0;
}
int temp, min;
int answer = 0;
boolean increasing;
if (prices[0] < prices[1]) {
increasing = true;
min = prices[0];
temp = prices[1] - prices[0];
} else {
increasing = false;
min = prices[1];
temp = 0;
}
for (int i = 2 ; i < n ; i ++) {
if (increasing) {
if (prices[i] > prices[i - 1]) {
temp = prices[i] - min;
} else {
answer += temp;
temp = 0;
min = prices[i];
increasing = false;
}
} else {
if (prices[i] < prices[i - 1]) {
min = prices[i];
} else {
temp = prices[i] - min;
increasing = true;
}
}
}
if (increasing) {
answer += prices[n - 1] - min;
}
return answer;
}
}