Swift simple O(n) solution


  • 0
    Z
     func maxArea(_ height: [Int]) -> Int {
            if (height.count <= 0) {
                return 0
            }
            var low = 0
            var high = height.count - 1
            var water = 0
            while (low < high) {
                let h = min(height[low], height[high]);
                water = max(water, (high - low) * h)
                while (low < high && height[low] <= h) {
                    low += 1
                }
                while (low < high && height[high] <= h) {
                    high -= 1
                }
            }
            return water
        }
    
    

Log in to reply
 

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