美文网首页
leetcode 11. Container With Most

leetcode 11. Container With Most

作者: 惺惺惜惺惺 | 来源:发表于2018-05-10 13:14 被阅读0次

    Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis 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 andnis at least 2.

    分析:

    题目大意是要找出组成容器的最大容量,容量的大小计算公式: area = (H[j]-H[i]) * min(H[i], H[j])    (i<=j)

    方法:容量大小取决于两根线的距离[X]和两根线当中小的那个min。

    可以考虑将 [X] 初始化为最大值,然后不断递减,当固定[X]时,min越大,容量也越大

    相关文章

      网友评论

          本文标题:leetcode 11. Container With Most

          本文链接:https://www.haomeiwen.com/subject/kjuzrftx.html