Best Time to Buy and Sell Stock III
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).
http://blog.youkuaiyun.com/u013027996/article/details/19414967
public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0){
return 0;
}
int len = prices.length;
int maxProfit = 0;
int min = prices[0];
int arrayA[] = new int[len];
for(int i = 1; i < len; i++){
arrayA[i] = prices[i] - min;
arrayA[i] = arrayA[i] > arrayA[i-1] ? arrayA[i] : arrayA[i-1];
if(prices[i] < min){
min = prices[i];
}
}
int max = prices[len-1];
int arrayB[] = new int[len];
for(int i = len-2; i >= 0; i--){
arrayB[i] = max - prices[i];
arrayB[i] = arrayB[i] > arrayB[i+1] ? arrayB[i] : arrayB[i+1];
if(prices[i] > max){
max = prices[i];
}
}
for(int i = 0; i < len; i++){
int tempValue = arrayA[i] + arrayB[i];
maxProfit = maxProfit > tempValue ? maxProfit : tempValue;
}
return maxProfit;
}
}
public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0){
return 0;
}
int len = prices.length;
int maxProfit = 0;
int min = prices[0];
int arrayA[] = new int[len];
for(int i = 1; i < len; i++){
arrayA[i] = prices[i] - min;
arrayA[i] = arrayA[i] > arrayA[i-1] ? arrayA[i] : arrayA[i-1];
if(prices[i] < min){
min = prices[i];
}
}
int max = prices[len-1];
int arrayB[] = new int[len];
for(int i = len-2; i >= 0; i--){
arrayB[i] = max - prices[i];
arrayB[i] = arrayB[i] > arrayB[i+1] ? arrayB[i] : arrayB[i+1];
if(prices[i] > max){
max = prices[i];
}
}
for(int i = 0; i < len; i++){
int tempValue = arrayA[i] + arrayB[i];
maxProfit = maxProfit > tempValue ? maxProfit : tempValue;
}
return maxProfit;
}
}