My Accepted Java Solution


  • 22
    W
    public class Solution {
    public int trap(int[] height) {
        if (height.length <= 2) return 0;
        int max = Integer.MIN_VALUE;
        int maxIndex = -1;
        for (int i = 0; i < height.length; i++) {
            if (height[i] > max) {
                max = height[i];
                maxIndex = i;
            }
        }
        
        int leftMax = height[0];
        int water = 0;
        for (int i = 1; i < maxIndex; i++) {
            if (height[i] > leftMax) {
                leftMax = height[i];
            } else {
                water += leftMax - height[i];
            }
        }
        
        int rightMax = height[height.length - 1];
        for (int i = height.length - 2; i > maxIndex; i--) {
            if (height[i] > rightMax) {
                rightMax = height[i];
            } else {
                water += rightMax - height[i];
            }
        }
        
        return water;
    }
    

    }


  • 0
    J

    wOW this is smart!


  • 0
    K

    fantastic job !


  • 0
    N

    awesome stuff!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.