public int trap_constant_space(int[] A) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (A == null || A.length == 0) return 0;

        int tallest = 0;
        for (int i = 0; i < A.length; i ++) {
            if (A[i] > A[tallest]) tallest = i;
        }

        int area = 0;
        int l = 0;
        for (int i = 0; i < tallest; i ++) {
            if (l > A[i]) area += l - A[i];
            else l = A[i];
        }
        int h = 0;
        for (int i = A.length - 1; i > tallest; i --) {
            if (h > A[i]) area += h - A[i];
            else h = A[i];
        }

        return area;
    }