博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode11. Container With Most Water (思路及python解法)
阅读量:2241 次
发布时间:2019-05-09

本文共 704 字,大约阅读时间需要 2 分钟。

Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.


根据一个数组计算两条线所包含的最大面积。

用两个指针,分别指向数组两端。

计算当前两条线段所包含的面积后,将较短的一条线段像中间移动。

因为短的线段已经用“满”了,也就是说,即使长的线段像中间移动,获得更长的线段,包含的面积也不可能增加了(面积计算受短线段限制)。

当然代码还可以用max()等方法简写,不再讨论。

class Solution:    def maxArea(self, height: List[int]) -> int:        l, r = 0, len(height)-1        final=0        while l
final: final=area return final

 

转载地址:http://jcrbb.baihongyu.com/

你可能感兴趣的文章
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】241-Different Ways to Add Parentheses
查看>>
【LEETCODE】312-Burst Balloons
查看>>
【LEETCODE】232-Implement Queue using Stacks
查看>>
【LEETCODE】225-Implement Stack using Queues
查看>>
【LEETCODE】155-Min Stack
查看>>
【LEETCODE】20-Valid Parentheses
查看>>
【LEETCODE】290-Word Pattern
查看>>
【LEETCODE】36-Valid Sudoku
查看>>
【LEETCODE】205-Isomorphic Strings
查看>>
【LEETCODE】204-Count Primes
查看>>
【LEETCODE】228-Summary Ranges
查看>>
【LEETCODE】27-Remove Element
查看>>
【LEETCODE】66-Plus One
查看>>
【LEETCODE】26-Remove Duplicates from Sorted Array
查看>>
【LEETCODE】118-Pascal's Triangle
查看>>