Leetcode 上面的 Best Time to Buy and Sell Stock III# JobHunting - 待字闺中
w*4
1 楼
总是过不了Large Judge。
各位大侠有更好的解法吗?
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0)
return 0;
int max =0;
for (int i = 0;i int value = maxProfit(prices,i);
max = Math.max(value, max);
}
return max;
}
private int maxProfit(int[] prices, int index){
int maxfront =0;
int min =0;
int maxend =0;
for (int i=0;i<=index;i++){
if (prices[i] min = i;
int diff = prices[i] - prices[min];
if (diff >maxfront){
maxfront = diff;
}
}
min = index+1;
for (int i = index+1;i if (prices[i] min = i;
int diff = prices[i] - prices[min];
if (diff >maxend){
maxend = diff;
}
}
return maxfront + maxend;
}
}
各位大侠有更好的解法吗?
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0)
return 0;
int max =0;
for (int i = 0;i
max = Math.max(value, max);
}
return max;
}
private int maxProfit(int[] prices, int index){
int maxfront =0;
int min =0;
int maxend =0;
for (int i=0;i<=index;i++){
if (prices[i]
int diff = prices[i] - prices[min];
if (diff >maxfront){
maxfront = diff;
}
}
min = index+1;
for (int i = index+1;i
int diff = prices[i] - prices[min];
if (diff >maxend){
maxend = diff;
}
}
return maxfront + maxend;
}
}