public int maxProfit(int[] prices) {
        if(prices.length == 0 || prices.length == 1) return 0;

        int[] left = new int[prices.length];
        int[] right = new int[prices.length];

        left[0] = 0;
        int min = prices[0];
        for(int i = 1; i < prices.length; i++){
            left[i] = Math.max(left[i - 1], prices[i] - min);
            min = prices[i] < min ? prices[i] : min;
        }

        right[prices.length - 1] = 0;
        int max = prices[prices.length - 1];
        for(int i = prices.length - 2; i >= 0; i--){
            right[i] = Math.max(right[i + 1], max - prices[i]);
            max = prices[i] > max ? prices[i] : max;
        }

        int maxProfit = 0;
        for(int i = 0; i < prices.length; i++){
            maxProfit = Math.max(maxProfit, left[i] + right[i]);
        }
        return maxProfit;
    }