Last active
September 10, 2023 15:24
-
-
Save charlespunk/4700452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Best Time to Buy and Sell Stock III Nov 7 '12 | |
Say you have an array for which the ith element is the price of a given stock on day i. | |
Design an algorithm to find the maximum profit. You may complete at most two transactions. | |
Note: | |
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int maxProfit(int[] prices) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
if(prices == null || prices.length < 2) return 0; | |
int minLR = prices[0]; | |
int maxRL = prices[prices.length - 1]; | |
int[] dpLR = new int[prices.length]; | |
int[] dpRL = new int[prices.length]; | |
int max = 0; | |
for(int i = 1; i < prices.length; i++){ | |
if(prices[i] < minLR) minLR = prices[i]; | |
dpLR[i] = Math.max(dpLR[i - 1], prices[i] - minLR); | |
} | |
for(int i = prices.length - 2; i > -1; i--){ | |
if(prices[i] > maxRL) maxRL = prices[i]; | |
dpRL[i] = Math.max(dpRL[i + 1], maxRL - prices[i]); | |
} | |
for(int i = 0; i < prices.length; i++){ | |
max = Math.max(max, dpLR[i] + dpRL[i]); | |
} | |
return max; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment