public int trap(int[] height) {
        if (height.length <= 1)
            return 0;
        int n = height.length;
        int[] maxLeft = new int[n];
        int[] maxRight = new int[n];
        int max = height[0];
        for (int i = 1; i < n; ++i) {
            maxLeft[i] = max;
            max = Math.max(height[i], max);
        }
        max = height[n - 1];
        int ans = 0;
        for (int i = n - 2; i > 0; --i) {
            maxRight[i] = max;
            max = Math.max(max, height[i]);
        }
        for (int i = 1; i < n - 1; ++i) {
            int min = Math.min(maxLeft[i], maxRight[i]);
            if (min > height[i]) {
                ans += (min - height[i]);
            }
        }
        return ans;
    }