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). ...
随机推荐
- 可持久化并查集 by zky
zz:https://www.cnblogs.com/cjoierljl/p/9567859.html https://www.cnblogs.com/peng-ym/p/9357220.html n ...
- 【Qt开发】Qt Creator在Windows上的调试器安装与配置
Qt Creator在Windows上的调试器安装与配置 如果安装Qt时使用的是Visual Studio的预编译版,那么很有可能就会缺少调试器(Debugger),而使用MSVC的Qt对应的原生调试 ...
- Go语言入门篇-高级数据类型
一.数组类型 二.切片类型 切片的更多操作方法 示例: 三.字典类型 四.通道类型 示例: 通道的更多种类 示例: 五.函数 示例: 六.结构体和方法 示例: 七.接口 八.指针 示例: mooc
- 关于R文件
1 什么是R文件 R文件是自动生成的文件,里面保存的是res目录下所有资源的ID. 2 如何使用 2.1 在java代码中使用 txtName = (TextView)findViewById(R.i ...
- 打印页面内容,<input>不好使,用<textarea> 代替
<textarea class="sld-textarea" onchange="changeTextareaValue(this)">123< ...
- Delphi中各个包中包含的控件
经常有朋友提这样的问题,“我原来在delphi5或者delphi6中用的很熟的控件到哪里去了?是不是在delphi7中没有了呢?这是不是意味着我以前写的代码全都不能够移植到delphi7中来了呢?是不 ...
- P4050 [JSOI2007]麻将
传送门 怎么好像没什么人写 $dp$ ...? 设 $f[i][j][k][0/1]$ 表示当前处理完前 $1$ 到 $i$ 的数,上一位开始的顺子有 $j$ 个,当前位开始的顺子有 $k$ 个,是否 ...
- 剑指offer-二叉搜索树与双向链表-python
题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.. # -*- coding:utf-8 -*- # class TreeN ...
- 使用axios发送ajax请求
1.安装 npm install axios 2.在Home.vue中引入 import axios from 'axios' export default { name: 'Home', c ...
- webpack配置--传统多页面项目
//依赖包--package.json文件 { "name": "webemeet", "version": "1.0.0&quo ...