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.

思路:要找到两根线使得容量最大

maxArea=min(height(begin),height(end))∗(end−begin)

贪心策略

思考:为什么贪心策略可以获得最优解?

我们所作的步奏是小的一端向中间紧缩,主要考虑这种策略是否会损失最有解,如下图



此时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的更多相关文章

  1. Container With Most Water - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...

  2. Container With Most Water -- LeetCode 11

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  3. Container With Most Water——LeetCode

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  4. Container With Most Water leetcode java

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

  7. 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 ...

  8. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  9. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  10. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...

随机推荐

  1. zuul的各种配置

    我们知道我们前台要展示数据给用户看,这中间可能涉及到从后端的多个微服务进行获取数据.比如获取用户信息需要用到用户微服务.获取商品信息需要获取商品微服务.创建订单需要调用订单微服务,而各个微服务可能分布 ...

  2. [BZOJ3307] 雨天的尾巴-----------------线段树进阶

    虽然是个板子,但用到了差分思想. Description N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最 ...

  3. 关于下载pyton第三方库的细节

    1.下载Python第三方库有时候国外的网站网速很不好,需要选择国内的镜像网站去下载 阿里云 http://mirrors.aliyun.com/pypi/simple   中国科技大学 https: ...

  4. JAVA笔记4__static关键字/对象数组/foreach/方法的可变参数

    /** * static关键字:修饰属性(实质就是全局变量).方法(无需本类的对象即可调用此方法).类. * 1.static方法只能调用static方法 * 2.static方法只能访问static ...

  5. ansible安装和批量执行命令

    yum install -y ansible 编辑 /etc/ansible/hosts 文件 # This is the default ansible 'hosts' file.## It sho ...

  6. split,cdn,shell脚本,tmux,记一次往国外服务器传大文件的经历

    需求是这样的:将一个大概680M的Matlab数据文件传到国外某所大学的服务器上,服务器需要连接VPN才能访问,由于数据文件太大,而且如果我直接ssh连过去或者用ftp传输,那么中间很可能中断. ps ...

  7. 《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)

    1.简介 在做web自动化时,有些情况selenium的api无法完成,需要通过第三方手段比如js来完成实现,比如去改变某些元素对象的属性或者进行一些特殊的操作,本文将来讲解怎样来调用JavaScri ...

  8. 【SVG】为了前端页面的美丽,我选择学习SVG

    [SVG]为了前端页面的美丽,我选择学习SVG 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 SVG在之前自学的过程中, ...

  9. 暑假算法练习Day5

    咕咕了好几天哈哈哈哈,因为这几天在忙一些其他事(bushi ,好吧其实就是自己太懒啦,从今天开始继续每天的算法练习 1010 一元多项式求导 (25 分) 设计函数求一元多项式的导数.(注:\(x^n ...

  10. gorm框架表名自动加s问题

    查看日志会发现表名自动加了s 在model实现以下方法即可解决 type UsUser struct { ID int64 `gorm:"column:id" db:"c ...