LeetCode?????????python?????????11. Container With Most Water
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode?????????python?????????11. Container With Most Water相关的知识,希望对你有一定的参考价值。
?????????www. ati height present href nta obj pytho ??????
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.
Example:
Input: [1,8,6,2,5,4,8,3,7] Output: 49
?????????????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????? = ??? * ??????????????????????????????????????????????????????
??????????????????????????????????????????????????? O???n^2??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????a = [2,8,6,1,5,4,8,3,7] a[0]???a[len(a)] ???????????????a[0]?????????????????????a[0]????????????????????????????????????a[len(a)], ?????????????????????a[0]?????????????????????????????????????????????a[0]?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????
class Solution(object): def maxArea(self, height): maxa = 0 front = 0 #?????????????????????????????? behind = len(height) - 1 #?????????????????????????????????????????????????????????????????????????????????????????????????????? while True: if front == behind: #??????????????????????????? break elif height[front] >= height[behind]: #???????????????????????????????????????????????????????????????????????????????????????????????? area = height[behind]*(behind - front) behind -= 1 if area > maxa: maxa = area else: #?????? area = height[front]*(behind - front) front += 1 if area > maxa: maxa = area return maxa
?????????????????????????????? O(n).
????????????????????????????????????????????????
Ry_Chen
以上是关于LeetCode?????????python?????????11. Container With Most Water的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 0079. Word Search单词搜索Python