Swift solution - Two Pointers


  • 0
    class Solution {
        func maxArea(_ height: [Int]) -> Int {
            var left = 0
            var right = height.count - 1
            var maxArea = 0
            
            while left < right {
                maxArea = max(maxArea, min(height[left], height[right]) * (right - left))
                if height[left] < height[right] {
                    left += 1
                } else {
                    right -= 1
                }
            }
            
            return maxArea
        }
    }
    

Log in to reply
 

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