30-Container With Most Water-Leetcode
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.
思路:要找到两根线使得容量最大
贪心策略
思考:为什么贪心策略可以获得最优解?
我们所作的步奏是小的一端向中间紧缩,主要考虑这种策略是否会损失最有解,如下图
此时end较小,end=end-1,这个步奏是否会遗漏最优解呢,如果是的话
,此时最优解以end结尾,但以end结尾的最大面积,也就是(begin,end)
这个面积我们已经计算过了,所以end = end -1,也就是说较小的一段向中间收缩的时候不会损失最优解,同理两端进行贪心操作是可以找到最优解的。
class Solution {
public:
int maxArea(vector<int>& height) {
int n = height.size();
int beg=0,end = n-1;
int max_area = 0;
while(beg<end)
{
int tmp=min(height[end],height[beg])*(end-beg);
if(max_area<tmp)max_area = tmp;
if(height[beg]<height[end]) beg++;
else end--;
}
return max_area;
}
};
30-Container With Most Water-Leetcode的更多相关文章
- Container With Most Water - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...
- Container With Most Water -- LeetCode 11
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Container With Most Water——LeetCode
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Container With Most Water leetcode java
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
随机推荐
- PCB电路板元器件布局的一般原则*(转)
PCB电路板元器件布局的一般原则: 设计人员在PCB电路板布局过程中需要遵循的一般原则如下. (1)元器件最好单面放置.如果需要双面放置元器件,在底层(Bottom Layer)放置插针式元器件, ) ...
- 21.10.9 test
T1 购票方案 \(\color{green}{100}\) 对于每个时间节点维护它作为每种票所能包含的最后一个点时,这种票的起始点位置,由于这个位置是单调的,所以类似双指针维护,\(O(KN)\) ...
- Qt 信号与槽的自动关联机制
前言 对于一些简单的事件判别,如点击按钮.无需写代码关联信号和槽函数. connect(ui->Btnshowhello,SIGNAL(clicked(bool)),this,SLOT(Btns ...
- MySQL之DDL数据定义语言:库、表的管理
库的管理 常用命令 #创建库 create database if not exists 库名 [ character set 字符集名]; create database if not exists ...
- 第2章-7 产生每位数字相同的n位数 (30分)
第2章-7 产生每位数字相同的n位数 (30分) 读入2个正整数A和B,1<=A<=9, 1<=B<=10,产生数字AA-A,一共B个A 输入格式: 在一行中输入A和B. 输出 ...
- geoserver控制服务访问权限-类似百度地图的key
目录 缘起 可行性分析 如何实现key验证访问 如何控制key能访问哪些地图服务? 如何实现服务器ip白名单 流程梳理 申请key 访问地图 实施步骤 拦截器设置 配置key验证规则 配置服务拦截规则 ...
- 关于使用idea工具debug时,断点颜色由红色变成灰色
在使用断点调试的时候,发现断点由原来的红色变成灰色的,后来发现是由于错误操作将Debug断点t调试禁用了 ,只需要点击禁用按钮取消就可以了
- c++学习笔记6(结构化程序设计的不足)
结构化程序设计 c语言使用结构化程序设计: 程序=数据结构+算法 程序有全局变量以及众多相互调用的函数组成 算法以函数的形式实现,用于对数据结构进行操作 结构化程序设计不足 软件业的目标是更快,更正确 ...
- python3排序 sorted(key=lambda)--实现对字典按value值排序
使用python对列表(list)进行排序,说简单也简单,说复杂也复杂,我一开始学的时候也搞不懂在说什么,只能搜索一些英文文章看看讲解,现在积累了一些经验,写在这里跟大家分享, 1.sorted函数首 ...
- [hdu7011]被EI加0了
注意到仅关心于权值大小,预处理出$F_{i}(n)$表示$a_{1},a_{2},...,a_{n}$中恰填$i$种不同的数的方案数,那么显然答案即为$\sum_{i=1} ...