| 3 | import "math" |
| 4 | |
| 5 | func maxArea(height []int) int { |
| 6 | left := 0 |
| 7 | right := len(height) - 1 |
| 8 | maxArea := 0 |
| 9 | |
| 10 | for left < right { |
| 11 | currentMax := (right - left) * |
| 12 | int(math.Min(float64(height[left]), float64(height[right]))) |
| 13 | maxArea = int(math.Max(float64(maxArea), float64(currentMax))) |
| 14 | |
| 15 | // We have two lines. The area is bounded by the shorter of the two. |
| 16 | // If we increase the pointer to the larger of the two, no larger and |
| 17 | // subsequent line will help maximize the area. If we keep the larger |
| 18 | // and increase the pointer to the smaller of the two, we stand the |
| 19 | // chance to maximize the area a bit more when subsequent lines are larger. |
| 20 | // So, incr/decr the line with the shorter height of the two. |
| 21 | if height[left] > height[right] { |
| 22 | right-- |
| 23 | } else { |
| 24 | left++ |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return maxArea |
| 29 | } |