leetcode | 最大装水量问题 | python
来源:互联网 发布:电脑主题壁纸软件 编辑:程序博客网 时间:2024/05/01 04:40
问题如下:
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.
给出一个数组,每个元素表示水桶高度,水桶的最终高度由数组中的两个元素组成(由两者较小的高度决定,短板效应),水桶的宽度由两个元素的下标差决定,求最大装水量。class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ i = 0 j = len(height)-1 water = 0 while i<j: water = max(water,(j-i)*min(height[i],height[j])) if height[i]<height[j]: i+=1 else: j-=1 return water其中i,j等同于数组的指针,算法的时间复杂度 O(n),每次只移动高度较小的指针。
阅读全文
0 0
- leetcode | 最大装水量问题 | python
- leetcode 计算最大盛水量
- 经典算法--最大存水量问题
- 最大盛水量
- 容器最大盛水量
- LeetCode Container With Most Water 查找容水量最大的容器 动态规划法思想分析
- Trapping Rain Water 求存水量 @LeetCode
- leetcode:最大容器问题
- LeetCode--Largest Number(最大数字)Python
- [LeetCode] Maximum Gap 最大间隙问题
- LeetCode Maximum subarray,最大子串问题
- LeetCode-水箱问题(求最大容量)
- 求字数组最大和问题 python
- Python实现求解最大子数组问题
- PAT 最大子列和问题 (Python)
- python 中最大,最小数的问题
- 最大子数组问题-递归法 @ Python
- leetcode 找到树中距离最大的两个结点,Python实现
- jquery的.post和 .ajax的区别
- Hibernate框架-联合主键映射
- POJ-1330(LCA算法之RMQ)
- arduino读取处理UWB(匿名科技的)数据集
- Glide加载图片并保存到本地返回file,bitmap
- leetcode | 最大装水量问题 | python
- charAt()使用
- Java网络编程——第九章 服务端Sokcet
- jQuery语法结构
- 给IT新人的15点建议:苦逼程序员的辛酸反省与总结
- Git简单生成生成公钥和私钥方法
- SQL Java + 数据库 知识点大全
- Java常用API(二)
- oracle RAC在某节点对部分表执行DML时遭遇TNS-12154报错