Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 1 ms, faster than 99.85% of Java online submissions for Best Time to Buy and Sell Stock III.
- // Memory Usage: 36.9 MB, less than 100.00% of Java online submissions for Best Time to Buy and Sell Stock III.
- class Solution {
- public int maxProfit(int[] prices) {
- int[] sell = { 0, 0 };
- int[] profit = {0, 0 };
- for (int i = prices.length - 1; i >= 0; i--)
- for (int j = 0; j < 2; j++) {
- profit[j] = Math.max(profit[j], -prices[i] + sell[j]);
- sell[j] = Math.max(sell[j], prices[i] + (j > 0 ? profit[j - 1]: 0));
- }
- return profit[1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement