public int maxProfit(int[] prices){
		if(prices == null || prices.length <= 1) return 0;
		
		// Calculate the max profit end at index i
		int[] forwardMaxProfits = new int[prices.length];
		int low = 0;
		int forwardMaxProfit = 0;
		
		for(int i=1; i<prices.length; i++){
			int profit = prices[i] - prices[low];
			if(profit > forwardMaxProfit)
				forwardMaxProfit = profit;
			
			if(prices[i] < prices[low])
				low = i;
			
			forwardMaxProfits[i] = forwardMaxProfit;
		}
		
		// Calculate the max profit between i and n-1, and calculate the final max profits
		int high = prices.length - 1;
		int backwardMaxProfit = 0;
		int finalMaxProfit = forwardMaxProfits[prices.length - 1];
		
		for(int i=prices.length - 1; i>=0; i--){
			int profit = prices[high] - prices[i];
			if(profit > backwardMaxProfit)
				backwardMaxProfit = profit;
			
			if(prices[i] > prices[high])
				high = i;
			
			int totalProfit = forwardMaxProfits[i] + backwardMaxProfit;
			if(totalProfit > finalMaxProfit)
				finalMaxProfit = totalProfit;
		}
		
		return finalMaxProfit;
		
	}