Accepted C# Solution


  • 0
    M
    public int Trap(int[] height) {
            var res = 0;
            int left = 0, right = height.Length - 1;
            int maxLeft = 0, maxright = 0;
            while (left < right)
            {
                if (height[left] < height[right])
                {
                    if (height[left] > maxLeft) maxLeft = height[left];
                    else res += maxLeft - height[left];
                    left++;
                }
                else
                {
                    if (height[right] > maxright) maxright = height[right];
                    else res += maxright - height[right];
                    right--;
                }
            }
            return res;
    }

Log in to reply
 

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