Description
LINK: https://leetcode.com/problems/container-with-most-water
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
Solution
1 | static int _ = []() { |
Discussion
思路很简单,从两头开始,不断缩小范围。
缩小的方法是,每次从较低的那一头缩小,一直缩小到第一个比它高的。然后再计算容积。