public static int trap(int[] A) {
        if (A == null || A.length < 2) return 0;
        int len = A.length;
        int sum = 0;
        int leftHeight[] = new int[len];
        int rightHeight[] = new int[len];
        int tempmax = 0;
        for (int i = 0; i < len; ++i) {
            leftHeight[i] = tempmax;
            if (A[i] > tempmax) {
                tempmax = A[i];
            }
        }
        Helpers.showArray(leftHeight);
        tempmax = 0;
        for (int i = len-1; i >= 0; --i) {
            rightHeight[i] = tempmax;
            int line = Math.min(leftHeight[i], rightHeight[i]);
            if (A[i] < line) {
                sum += (line - A[i]);
            }
            //update tempmax
            if (A[i] > tempmax) {
                tempmax = A[i];
            }
        }
        System.out.println(sum);
        return sum;
    }