122. Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II
Solution
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) return 0;
int[] dp = new int[prices.length];
for (int i = 1; i < prices.length; i++)
dp[i] = prices[i] - prices[i - 1];
int result = 0;
for(int i = 0; i < dp.length; i ++) {
if (dp[i] > 0) {
result += dp[i];
}
}
return result;
}
}Last updated