Leetcode 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.
Note: You may not slant the container and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49 这个题想了很久,结果没想到怎么做,其实现在理解以下就是一个双边贪心的策略,尽量的宽,又尽力的高才能存储更多的水,所以,从两端向中间贪心,如果左边的比右边的高就更新右边的边,往中间靠一阶,看能不能得到更好的,同理,如果右边的比左边的短,右边就向右扫描一个,如果相等,那随便选择一边更新一格就可以。
class Solution {
public:
int maxArea(vector<int>& height) {
int r = ; int l = height.size()-;
int Max = (min(height[l],height[r])*(l-r));
while(r<l){
if(height[r] <= height[l]) r++;
else if(height[l] < height[r]) l--;
Max = max(Max,(min(height[l],height[r])*(l-r)));
}
return Max;
}
};
Leetcode 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 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- LeetCode 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). ...
- [LeetCode]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, ...
- Java [leetcode 11] Container With Most Water
问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- C#解leetcode 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 My Submissions Question 解题思路
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- JavaSE_Java跨平台原理
Java语言的核心优势就是跨平台. C/C++语言都是直接编译成针对特定平台的机器码,如果要跨平台,需要借用相应的编译器重新编译.Java源程序(.java)要先编译成与平台无关的字节码文件(.cla ...
- vue插槽用法(极客时间Vue视频笔记)
vue插槽 插槽是用来传递复杂的内容,类似方法 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- [转帖]16nm国内最先进 兆芯展示x86 KX-6000八核处理器
16nm国内最先进 兆芯展示x86 KX-6000八核处理器 https://www.cnbeta.com/articles/tech/847125.htm 在近日的2019北京国际互联网科技博览会暨 ...
- Linux yum 命令篇
yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器. 基於RPM包管理,能够从指定的服务器自动下载RPM包 ...
- Linux hostname 主机名篇
主机名修改(以主机名为config为例) 1.修改文件/etc/sysconfig/network,内容为 [root@config ~]# cat /etc/sysconfig/network# C ...
- [LeetCode] 203. 移除链表元素
题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/ 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输 ...
- [pwnable.kr] - wtf
Q: I don't understand why my exploit is not working. I need your help. download : http://pwnable.kr/ ...
- PY个树状数组
树状数组看起来比较简单,于是就挑它下手了... 于是生活终于也对咱下手了... 要讲的就两个东西,一个是开数组,全局变量写最前面,数组是这么开的: f=[0 for i in range(500005 ...
- vscode学习(三)之如何修改打开终端的默认shell
实现 第一步:打开VSCode的设置(Preferences>User Settings) 第二步:搜索terminal.integrated.shell.osx 的 并把它的值改为你的zsh安 ...
- linux基本命令之磁盘管理命令(ls,cd,pwd,mkdir,rmdir,clear, touch)
linux磁盘管理命令 1.ls(list)命令:列出目录内容. 格式:ls [参数][文件或目录] ls -a或-all表示列出所有文件和目录,以点开始的是影藏文件,例如,.bash_history ...