Container With Most Water——双指针
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.
用两个指针从数组的左边和右边开始,向中间搜索。依据的理由有两点:
1、因为一开始底边长就已经是最大,两个指针向中间移动的时候,底边长只会变短,因此如果此时面积要变大的话,只能是两条高中的最短者比移动前的最短高更高,
2、因此,每次选择移动左指针还是右指针,优先选择当前高度最短的那个,以期寻找到更高的边。如果此时选择移动当前高度最高的那个,就有可能跳过了最优解。
直观的解释是:容积即面积,它受长和高的影响,当长度减小时候,高必须增长才有可能提升面积,所以我们从长度最长时开始递减,然后寻找更高的线来更新候补;
没怎么用过Python写过程序,所以决定用Python来写写,结果错误百出。
出错的地方如下:
1,python在for循环,while,if等语句中不用括号,应该用缩进和:
2,和c++一样python需要特比的注意中文输入,这个程序就是有个中文的:,害得我一直不知道错在哪.
class Solution:
# @param {integer[]} height
# @return {integer}
def maxArea(self, height):
Maxlenth=len(height)
if == Maxlenth:
return
StartPos=
MaxLeft=height[StartPos]
EndPos=Maxlenth-
MaxRight=height[EndPos]
MaxCont=
while StartPos<EndPos:
if height[StartPos]>=height[EndPos]:
T=height[EndPos]*(EndPos-StartPos)
EndPos-=
if height[EndPos]>MaxRight:
MaxRight=EndPos
else:
T=height[StartPos]*(EndPos-StartPos)
StartPos+=
if height[StartPos]>MaxLeft:
MaxLeft=StartPos
if T>MaxCont:
MaxCont=T
return MaxCont
Container With Most Water——双指针的更多相关文章
- Container With Most Water - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 67. Container With Most Water
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- 关于Container With Most Water的求解
Container With Most Water 哎,最近心情烦躁,想在leetcode找找感觉,就看到了这题. 然而,看了题目半天,硬是没看懂,于是乎就百度了下,怕看到解题方法,就略看了下摘要,以 ...
随机推荐
- 使用canvas画一个雷达效果图的特效代码
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...
- POJ3694:Network(并查集+缩点+lca)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13172 Accepted: 4774 题目链接:htt ...
- HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- Java重要知识点
1.Java中除了static方法和final方法之外,其它所有的方法都是动态绑定,如同C++的虚函数,但是我们不需要显示的声明. private方法本质上属于final方法(因此不能被子类访问). ...
- [转载][mysql]mysql字符集干货
源地址:http://www.blogjava.net/zyskm/archive/2013/04/09/361888.html 字符集的概念大家都清楚,校对规则很多人不了解,一般数据库开发中也用不到 ...
- jquery多组图片层次切换的焦点图
效果:
- [LeetCode] 3. Longest Substring Without Repeating Characters ☆☆☆
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- nginx 负载均衡实现
https://www.cnblogs.com/wang-meng/p/5861174.html
- vijos 1464 积木游戏 DP
描述 积木游戏 SERCOI 最近设计了一种积木游戏.每个游戏者有N块编号依次为1 ,2,…,N的长方体积木.对于每块积木,它的三条不同的边分别称为"a边"."b边&qu ...