11. 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.
思路:每次移动矮的那侧的指针,并且只关心比原来高的情况(因为宽度已经减小了,高度必须增高才可能面积变大)
int maxArea(int* height, int heightSize) {
int start = ;
int end = heightSize-;
int ret = ;
int maxStart = ;
int maxEnd = ; while(start < end){
if(height[start] < height[end]){
if(height[start]*(end-start) > ret) ret = height[start]*(end-start);
while(start<end){
start++;
if(height[start] > maxStart){
maxStart = height[start];
break;
}
}
}
else{
if(height[end]*(end-start) > ret) ret = height[end]*(end-start);
while(start<end) {
end--;
if(height[end] > maxEnd){
maxEnd = height[end];
break;
}
}
}
} return ret;
}
11. Container With Most Water(头尾双指针)的更多相关文章
- 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 Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [leecode]---11.container with most water
description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 11. Container With Most Water(装最多的水 双指针)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- leetcode985
import sys class Solution: def sumEvenAfterQueries(self, A: 'List[int]', queries: 'List[List[int]]') ...
- linux RPM包管理
查询系统是否安装某个应用 rpm -qa | grep xx 查询系统某个应用的版本信息 rpm -qi 软件包信息 查询某个软件的安装位置 rpm -ql 软件包名 查询文件属于哪个软件 ...
- First changce exceptoin
C++,改一点代码,F9,报一串地址错. First changce exceptoin是啥原因 退出也rad重进也不行,只能clean工程,完整编译才可以.感觉是没有把最新修改编译链接. 有 ...
- J2SE 8的流库 --- 转换流, 得到的还是流
流的转换, 按照条件过滤/映射/摊平/截取/丢弃/连接/去重/排序. 辅助方法 public static int myCompare(String x, String y) { if(x.lengt ...
- curl发送xml , xml和数组互转
public function postXml($url, array $data) { // pack xml $xml = $this->arrayToXml($data); // curl ...
- java序列化问题
今天无意中看到了 redistemplet封装的类中,出现了序列化的字眼 ,了解下序列化的知识 1.什么是序列化 我们把变量从内存中变成可存储或传输的过程称之为序列化,(java中一般是用来保 ...
- linux 2.6.32文件系统的inode
接上文: crash> struct -xo dentry.d_inode ffff8818118002c0 struct dentry { [ffff8818118002d0] struct ...
- JAVAWEB 一一 Spirng(AOP面向切面)
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <!-- < ...
- C++ CBitmap,HBitmap,Bitmap区别及联系
加载一位图,可以使用LoadImage: HANDLE LoadImage(HINSTANCE hinst,LPCTSTR lpszName,UINT uType,int cxDesired,int ...
- Unity3D初学之2D动画制
作者:Alex Rose Unity最近宣布推出额外的2D游戏支持,添加了Box 2D物理和一个精灵管理器. 但这里还是有些技巧需要牢记在心.逐帧更改图像只是动画制作的冰山一角,若要让你的游戏出色运行 ...