C# solution: local & global, shrink two pointers


  • 1
    B
    public class Solution 
    {
        public int MaxArea(int[] height) 
        {
            if (height.Length < 2) return 0;
    
            var left = 0;
            var right = height.Length - 1;
    
            var globalArea = 0;
    
            while(left < right)
            {
                var localArea = Math.Min(height[left], height[right]) * (right - left);
    
                globalArea = Math.Max(localArea, globalArea);
    
                if (height[left] > height[right])
                {
                    right--;
                }
                else if (height[left] < height[right])
                {
                    left++;
                }
                else
                {
                    left++;
                    right--;
                }
            }
    
            return globalArea;
        }
    }
    

Log in to reply
 

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