All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0011_container_with_most_water.solution.go Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package s0011_container_with_most_water

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1)
// #2024_03_07_Time_60_ms_(94.99%)_Space_8.3_MB_(24.10%)

func maxArea(height []int) int {
	l, r := 0, len(height)-1
	maxArea := 0

	for l < r {
		area := min(height[l], height[r]) * (r - l)
		if area > maxArea {
			maxArea = area
		}

		if height[l] < height[r] {
			l++
		} else {
			r--
		}
	}

	return maxArea
}

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy