Contents

[Leetcode] 11. Container With Most Water

Contents

https://leetcode.com/problems/container-with-most-water/description/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
func maxArea(height []int) int {
    max := 0
    left := 1
    right := len(height)

    for left < right {
        y := min(height[left - 1], height[right - 1])
        x := right - left
        area := x * y
        
        if max < area {
            max = area
        }

        if height[left - 1 ] <= height[right - 1] {
            left += 1
        } else {
            right -= 1
        }

    }
    return max
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}