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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package main
import "fmt"
func maxArea(height []int) int { maxArea := 0 left, right := 0, len(height)-1 for left < right { w := right - left h := min(height[left], height[right]) area := w * h if maxArea < area { maxArea = area } if height[left] < height[right] { left ++ } else { right -- } }
return maxArea }
func main() { testCases := []struct { height []int expected int }{ {[]int{1,8,6,2,5,4,8,3,7}, 49}, {[]int{1,1}, 1}, }
for i, tc := range testCases { result := maxArea(tc.height) fmt.Printf("Test Case %d, Input: height = %v\n", i+1, tc.height) if result == tc.expected { fmt.Printf("Test Case %d, Output: %d, PASS\n", i+1, result) } else { fmt.Printf("Test Case %d, Output: %d, FAIL (Expected: %d)\n", i+1, result, tc.expected) } } }
|