feat: sunday
This commit is contained in:
36
pkg/container_with_most_water/main.go
Normal file
36
pkg/container_with_most_water/main.go
Normal file
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user