Continue learning
Practice related DSA questions in the same topic.
Two vertical walls enclose width times the shorter height. Return the greatest area among all pairs.
Two vertical walls enclose width times the shorter height. Return the greatest area among all pairs.
def solve(values, target): left = 0 right = (len(values) - 1) best = 0 while (left < right): area = (min(values[left], values[right]) * (right - left)) best = max(area, best) if (values[left] <= values[right]): left = (left + 1) else: right = (right - 1) return bestCompare this container area 8 with the current best.
Compare this container area 8 with the current best.
A reservoir spills over the lower of its two retaining walls.
Moving the taller wall cannot improve the area while the shorter wall is fixed.
O(n) time · O(1) auxiliary space
Worked example · {"values":[2,8,3,6,5],"target":0} ⇒ 15
Edge case 1 · {"values":[],"target":0} ⇒ 0
Edge case 2 · {"values":[4],"target":0} ⇒ 0
Height alone ignores the distance between walls.
Does the choice matter when both walls have equal height?