| 1 | class Solution { |
| 2 | public int maxProfit(int[] prices) { |
| 3 | |
| 4 | int n=prices.length; |
| 5 | int left[] = new int[n]; |
| 6 | int right[]= new int[n]; |
| 7 | int lmin=prices[0],rmax=prices[n-1]; |
| 8 | // First purchase |
| 9 | |
| 10 | for(int i=1;i<n;i++) |
| 11 | { |
| 12 | left[i] = Math.max(left[i-1],prices[i]-lmin); |
| 13 | lmin=Math.min(lmin,prices[i]); |
| 14 | } |
| 15 | // Second purchase |
| 16 | for(int i=n-2;i>=0;i--) |
| 17 | { |
| 18 | right[i] = Math.max(right[i+1],rmax-prices[i]); |
| 19 | rmax=Math.max(rmax,prices[i]); |
| 20 | } |
| 21 | |
| 22 | int profit=right[0]; |
| 23 | |
| 24 | for(int i=1;i<n;i++) |
| 25 | { |
| 26 | profit=Math.max(profit,left[i-1]+right[i]); |
| 27 | } |
| 28 | return profit; |
| 29 | |
| 30 | |
| 31 | } |
| 32 | } |
nothing calls this directly
no outgoing calls
no test coverage detected