题目:

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.

题意:

给定n个非负整数a1, a2,
..., an,
每一个代表在坐标(i, ai)上的一点,连接(i, ai)
and (i, 0)形成n条垂直线。找出两条垂线,和x坐标形成一个容器,使得这个容器包括的水最多。

注意:你不能够倾斜容器。

算法分析:

两边夹的策略

两个指标i j往中间走。每次计算i和j之间的面积。假设比眼下最大面积大。则更新最大面积,否则让两者之间较小的数的指标往前走。

假设height[i] <= height[j],那么i++,由于在这里height[i]是瓶颈,j往里移仅仅会降低面积,不会再添加area。

这是一个贪心的策略,每次取两边围栏最矮的一个推进,希望获取很多其它的水。

AC代码:

    public  int maxArea(int[] height)
{
int left=0;
int right=height.length-1;
int temwater;
int res=0;
while(left<right)
{
temwater=Math.min(height[left], height[right])*(right-left);
if(temwater>res) res=temwater;
if(height[left]<height[right])
left++;
else
right--;
}
return res;
}

[LeetCode][Java] Container With Most Water的更多相关文章

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

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

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

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

  3. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  5. 【JAVA、C++】LeetCode 011 Container With Most Water

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

  6. Java [leetcode 11] Container With Most Water

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

  7. LeetCode 11. Container With Most Water (装最多水的容器)

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

  8. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  9. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

随机推荐

  1. 菜鸟的《Linux程序设计》学习——MySQL数据库安装、配置及基本操作

    1. MySQL数据库: 在涉及到一些大型的Web系统或者嵌入式软件的开发时,都少不了用数据库来管理数据.在Windows操作系统下,使用过各种各样的数据库,如:sqlServer.Oracle.My ...

  2. Codeforces Round #877 (Div. 2) B. - Nikita and string

    题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...

  3. H.264基本原理与编码流程

    H264视频压缩算法现在无疑是所有视频压缩技术中使用最广泛,最流行的.随着 x264/openh264以及ffmpeg等开源库的推出,大多数使用者无需再对H264的细节做过多的研究,这大降低了人们使用 ...

  4. Hive 将本地数据导入hive表中

    # 导入 load data local inpath '/root/mr/The_Man_of_Property.txt' insert into table article; # 提示 FAILE ...

  5. Leetcode 357.计算各个位数不同的数字个数

    计算各个位数不同的数字个数 给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n . 示例: 输入: 2 输出: 91 解释: 答案应为除去 11,22,33 ...

  6. HDU-1030 Delta-wave ,暴力贪心!

    Delta-wave 题意:用图中所示的数字填满这个三角形,求n到m的最短路径,也就是最少通过几条边. 思路:简单贪心就可以了,本人是先将每一层的左右端点用结构体存起来,然后分奇偶判断. 这题必须理清 ...

  7. rabbitmq php 学习

    参考文档:http://www.cnblogs.com/phpinfo/p/4104551...http://blog.csdn.net/historyasamirror/ar... 依赖包安装 yu ...

  8. BZOJ 1294 [SCOI2009]围豆豆Bean ——计算几何

    显然我们不可能表示出一台路径,因为实在是太复杂了. 所以我们可以记录一下路径对答案的影响,显然路径对答案影响相同的时候,答案更优,所以我们可以用影响来代替路径. 所以我们考虑状压一下所有的豆子有没有被 ...

  9. BZOJ 3282 Tree ——Link-Cut Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...

  10. Ubuntu 常用命令和一些 tips

    001. ubuntu 解压.tar.xz文件到另一个文件夹:sudo tar -xvJf ***.tar.xz -C /usr/src sudo 超级用户tar [选项...][file]...-x ...