Thought:
We just bought on the first day, and every time we can earn some money we sell it.
Code:
public class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
if(prices.length == 0) return profit;
int bought = prices[0];
for(int i = 1; i < prices.length; i++) {
if( prices[i] > bought){
profit += prices[i] - bought;
}
bought = prices[i];
}
return profit;
}
}
No comments:
Post a Comment