public int trap(int[] A) {
        if(A.length <= 2) return 0;

        //Find left most height for each bar
        int[] left = new int[A.length];
        left[0] = 0;
        for(int i = 1; i < A.length; i++){
            left[i] = Math.max(left[i - 1], A[i - 1]);
        }

        //Find right most height for each bar
        int[] right = new int[A.length];
        right[A.length - 1] = 0;
        for(int i = A.length - 2; i >= 0; i--){
            right[i] = Math.max(right[i + 1], A[i + 1]);
        }

        int water = 0;
        for(int i = 0; i < A.length; i++){
            if(Math.min(left[i], right[i]) - A[i] > 0){
                water += Math.min(left[i], right[i]) - A[i];
            }
        }
        return water;
    }