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. Kruskal重构树-进阶

    例题一:区间最小生成树(NKOJ P8439) 简要题意: 一个n个点m条边的无向图,点编号1到n,边编号1到m.边有边权. 有q次操作,操作分两种: 1.k x y z:修改第k条边,使其连接的两点 ...

  2. HZOI帝国2019欢乐时刻

    前言: update 只是恢复一下原来手残删掉的博客,不是在水,嘤嘤嘤 update 以后改成Stack,但是之前的就懒得改了... by 10.31 为了窝的访问量大家的好心情,模仿学长搞了一个这个 ...

  3. 洛谷 P2221 [HAOI2012]高速公路

    链接: P2221 题意: 有 \(n(1\leq n\leq 10^5)\) 个点,从第 \(i(1\leq i< n)\) 个点向第 \(i+1\) 个点连有边.最初所有边长 \(v_i\) ...

  4. Android 服务名称规则invalid service name 限制16字符以内

    今天调试网络服务的时候为了区分,修改了原有服务名称,同时新增了两个服务. 系统运行的时候报错找不到对应的服务 init: no such service 'wpa_supplicant_common' ...

  5. linux 内核源代码情景分析——越界访问

    页式存储管理机制通过页面目录和页面表将每个线性地址转换成物理地址,当遇到下面几种情况就会使CPU产生一次缺页中断,从而执行预定的页面异常处理程序: ① 相应的页面目录或页表项为空,也就是该线性地址与物 ...

  6. SpringCloud微服务实战——搭建企业级开发框架(十五):集成Sentinel高可用流量管理框架【熔断降级】

      Sentinel除了流量控制以外,对调用链路中不稳定的资源进行熔断降级也是保障高可用的重要措施之一.由于调用关系的复杂性,如果调用链路中的某个资源不稳定,最终会导致请求发生堆积.Sentinel ...

  7. Kioskcached(2) 之 使用tcmalloc 替换 ptmalloc

    前言 我在 Kioskcached(1)之 Memcached & Redis & Kioskcached 性能测试对比 中找到的一个问题是 malloc,对于一个内存型数据库,很容易 ...

  8. 用Python实现人像动漫化

    用Python实现人像动漫化 本文章会教你如何使用Python实现人像动漫化,先看看效果实例: 实现过程如下: 本案例是使用百度的API来实现的,首先需要进入百度AI开放平台注册账号,具体流程就不讲了 ...

  9. Part 27 Remove # from URL AngularJS

    There are 4 simple steps to remove # from URLs in Angular. Step 1 : Enable html5mode routing. To do ...

  10. 9组-Alpha冲刺-2/6

    一.基本情况 队名:不行就摆了吧 组长博客:https://www.cnblogs.com/Microsoft-hc/p/15534079.html 小组人数: 8 二.冲刺概况汇报 谢小龙 过去两天 ...