11.Container With Most Water (Array; Two-Pointers)
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.
思路:两个指针指向开头和结尾的位置,每次将矮的那侧的指针往中间移动,并且只关心比原来高的情况(因为宽度已经减小了,高度必须增高才可能面积变大)
class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
int left = , right = height.size()-;
int maxLeft = , maxRight = ;
int ret = ;
while(left < right){
if (height[left] < maxLeft) {
left++;
continue;
}
else maxLeft = height[left];
if (height[right] < maxRight) {
right--;
continue;
}
else maxRight = height[right];
ret = max(ret, min(height[left], height[right]) * (right - left));
if(height[left] < height[right]) left++;
else right--;
}
return ret;
}
};
11.Container With Most Water (Array; Two-Pointers)的更多相关文章
- 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 ...
- Leetcode Array 11 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode 11. [👁] Container With Most Water & two pointers
盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
随机推荐
- day18 python模块 random time sys os模块
day18 python 一.random模块 取随机整数 import random print(random.randint(1,2)) #顾头顾尾 p ...
- jsonp跨域获取数据实现百度搜索
本菜鸡最近在写某个页面请求数据时,报了如下的错误. Failed to load https://...:No 'Access-Control-Allow-Origin' header is pres ...
- python基础:6.python最大的递归层数
python解释器版本:3.7 def recursion(n): print(n) n += 1 recursion(n) recursion(1) # maximum recursion dept ...
- SQL必知必会学习笔记
2.5 select SELECT 要返回的列或表达式 是FROM 从中检索数据的表 仅在从表选择数据时使用WHERE 行级过滤 ...
- Centos7.4 离线安装httpd(解决rpm依赖)
1.直接下载httpd的rpm安装包,安装失败需要先解决依赖. [root@node06 ~]# rpm -ivh httpd--.el7.centos.x86_64.rpm warning: htt ...
- npm 常见错误记录
1.Module build failed: ReferenceError: Unknown plugin "import" specified in "base&quo ...
- myeclipse svn重新定位 本地文件 svn 重新定位
我们在用工具myeclipse开发项目时,当资源库存储空间不够时,我们就需要添加资源库,涉及到我们切换项目资源库,下面就介绍一下svn资源库重新定位步骤 1,window到show view到othe ...
- Linux配置postfix
启动报错:主机名不能以数字开头,否则报错
- [CSP-S模拟测试]:喝喝喝(模拟)
题目描述 奥利维尔和雪拉扎德在喝酒.两人连喝$18$瓶后,奥利维尔最终倒下了.奥利维尔服用了教会研究的醒酒药后,因为服用了太多产生了副作用,第二天睡不着了.他只好用数数的方式度过无聊的时光,不过他毕竟 ...
- git add 添加错文件的撤销方法
git add 添加 多余文件 这样的错误是由于,有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤销操作 ...