[leetcode: Python]11. Container With Most Water
来源:互联网 发布:什么是数据资源 编辑:程序博客网 时间:2024/06/01 08:06
title:
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.
题意:
areaMax = min(height[i], height[j])*abs(i - j)
方法一:65ms
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ left = 0 right = len(height) - 1 res = 0 while left < right: water = min(height[left], height[right]) * (right - left) if water > res: res = water if height[left] < height[right]: left += 1 else: right -= 1 return res
方法二:65ms
class Solution(object): def maxArea(self, height): ans = 0 area = 0 i = 0 j = len(height)-1 while i < j: if height[i] < height[j]: area = height[i] * (j - i) i += 1 else: area = height[j] * (j - i) j -= 1 ans = max(area, ans) return ans
阅读全文
0 0
- [leetcode: Python]11. Container With Most Water
- 【LeetCode with Python】 Container With Most Water
- Leetcode -- Python --Container With Most Water
- 【LeetCode】【Python题解】Container with most water
- 10.Container With Most Water Leetcode Python
- 【LEETCODE】11-Container With Most Water [Python]
- 【leetcode】Container with most water--python实现
- Container With Most Water Leetcode Python Java
- [Leetcode] Container With Most Water Python
- 【Leetcode】【python】Container With Most Water
- 11. Container With Most Water leetcode Python 2016 new Season
- Leetcode 11. Container With Most Water The Solution of Python
- python写算法题:leetcode: 11. Container With Most Water
- LeetCode 11. Container With Most Water (Python)
- LeetCode 11. Container With Most Water
- [LeetCode]11.Container With Most Water
- LeetCode --- 11. Container With Most Water
- LeetCode 11.Container With Most Water
- C语言进阶之二级指针的应用:重置内存空间大小
- Android 避免APP启动闪黑屏的解决办法(Theme和Style)
- 【51Nod】1087 1 10 100 1000
- 如何唤醒socket被阻塞的函数
- Vue2.0 + Element-UI + WebAPI实践:简易个人记账系统
- [leetcode: Python]11. Container With Most Water
- vue组件的3种书写形式
- js开发:正则实例详解
- myBatis获取SqlSession连接对象的两种方式
- 安全控件开发原理分析 支付宝安全控件开发 网银密码控件 C++
- Android Service解析(二):远程Service的使用
- Java容器ArrayList源代码解析
- linux学习第一课(思维导图)
- Linux下zip和unzip解压缩文件命令