From f3abe4ff5bedc75284e00cad7947d9e00522ae6f Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Sun, 11 Jan 2026 15:42:42 -0500 Subject: [PATCH] feat: sunday --- pkg/container_with_most_water/main.go | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 pkg/container_with_most_water/main.go diff --git a/pkg/container_with_most_water/main.go b/pkg/container_with_most_water/main.go new file mode 100644 index 0000000..6bc1f18 --- /dev/null +++ b/pkg/container_with_most_water/main.go @@ -0,0 +1,36 @@ +package container_with_most_water + +func MaxArea(height []int) int { + if len(height) < 2 { + return 0 + } + + start := 0 + end := len(height) - 1 + + // Let: + // - A = the smaller side, + // - B = the larger side. + // + // Notice that: + // - Any container with A cannot have a height larger than `min(A, B)`. + // - Any container with A cannot have a width larger than `B - A`. + // + // Because of this, all solutions containing A cannot be larger than `area`. + area := (end - start) * min(height[start], height[end]) + + // Futhermore, we can completely ignore A. + // + // The maximum area will either be the current `area`, or some other two + // lines in the subset without A. + var subset []int + if height[start] < height[end] { + subset = height[start+1:] + } else { + subset = height[:end] + } + + // We can now evaluate the problem space in the subset, and compute the + // maximum area. + return max(area, MaxArea(subset)) +}