leetcode 11. Container With Most Water (two pointer)

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.

题意:n条竖直的线段 (i,0)->(i,a[i]),从中选2条,和x轴共同组成一个开口的容器,问容器的最大面积。

思路:一开始想错了,以为是最大连续矩形面积…还在想leetcode竟然考单调栈???

然而实际上只取两个线段,中间的线段有没有,长度,都是无所谓的。

因此two pointer就好了。。。

 

说点什么

您将是第一位评论人!

提醒
wpDiscuz