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).找 ...
随机推荐
- vue-如何输出Hello world
首先引入vue.js文件 <script src="vue.js"></script> <!DOCTYPE html> <html> ...
- Vue-系统修饰键
可以用如下修饰符来实现仅在按下相应按键时才触发鼠标或键盘事件的监听器. .ctrl .alt .shift .meta 例如: 例如: <!-- Alt + C --> <input ...
- 开源Futter项目
前段时间Flutter很火,所以在闲暇之余做了一个助学通的Flutter移动端应用,现在开源出来,希望对想要学习Flutter的朋友有所帮助. 我大致做个项目介绍: 学生签到系统:分java服务端提供 ...
- 苹果的AR赌注仍然有很多需要证明的
苹果公司为开发者主题发布会做准备,其中一个更大的公告很可能是其增强现实平台的新变化.自从去年宣布ARKit以来,这家科技巨头几乎对其对AR的潜力抱有信心. 在很多讨论背后,人们都相信技术的实用性,但在 ...
- hive的数据定义之创建数据库和表
1.对数据库的操作 create database hive_db //创建数据库hive_db create table hive_db.test(字段内容及其格式省略) //在数据库hive_db ...
- STM32F103单片机学习—— 通用定时器
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fengshuiyue/article/details/79150724 本篇重点记录的是STM32F ...
- vs2005下opengl(glut)的配置记录
摘自:http://blog.csdn.net/joeblackzqq/article/details/6956959 首先参考了网上的安装配置环境部分:http://blog.csdn.net/Id ...
- git操作命令行
前言 git操作各种软件五花八门,懒得研究,用最原始的方法敲命令行. 操作 1.网上下载git 网上百度一下好多直接下载就好 2.配置用户名邮箱 $ git config --global user. ...
- Delphi 运行后错误提示“无效的授权说明”
Delphi 运行后错误提示“无效的授权说明” 一般情况是:数据库的连接出现了问题. 解决方法:检查加载数据库是否正常,能否正常连接.
- delphi 使用 InputBox、InputQuery 的启发
使用 InputBox.InputQuery 的启发 看了 InputBox.InputQuery 函数实现的源码, 有些收获与心得... ------------------------------ ...