欢迎24级新生

2083. LeetCode 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. The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

输入

Input: [1,8,6,2,5,4,8,3,7]

输出

Output: 49

提示

给出⼀个⾮负整数数组 a1,a2,a3,…… an,每个整数标识⼀个竖⽴在坐标轴 x 位置的⼀堵⾼度为 ai的墙,选择两堵墙,和 x 轴构成的容器可以容纳最多的⽔。 这⼀题也是对撞指针的思路。⾸尾分别 2 个指针,每次移动以后都分别判断⻓宽的乘积是否最⼤。

登录以提交代码。
单点时限 1 秒
内存限制 128 MB
提交 0
通过 0