public int maxProfit(int[] prices) {
        int length = prices.length;
        if(length == 0)
            return 0;
        int[] history = new int[length];
        int[] furture = new int[length];
        int maxProfit=0;
        int valley = prices[0];
        int peak = prices[length-1];
        for(int i =0 ; i< length;i++){
            valley = Math.min(valley,prices[i]);
            if(i>0)
                history[i] = Math.max(history[i-1],prices[i]-valley);
        }
        for (int i =length-1;i > 0 ; i--) {
            peak = Math.max(peak,prices[i]);
            if(i<length-1)
                furture[i] = Math.max(furture[i+1],peak- prices[i]);
            maxProfit = Math.max(maxProfit,furture[i] + history[i]);

        }
        return maxProfit;
    }