1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
class Solution { public int maxProfit(int[] prices) { int len =prices.length; if (len==0){ return 0; } int max = 0; for(int i=0;i<len;i++){ int leftLen = i; int rightLen = len-i; int left = compute(prices,0,i); int right = compute(prices,i,len); if (max<left+right){ max = left+right; } } return max; } public int compute(int[] prices,int start,int end) { if (end-start<=0){ return 0; } int min = prices[start]; int max = 0; for(int i=start+1;i<end;i++){ int price = prices[i]; if (price-min>max){ max = price-min; } if (price<min){ min = price; } } return max; } }
|
近期评论