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)上的一个点,n条直线的两端各自是(i,ai) ,(i,0)。找出两条直线,使其与x轴形成一个容器,使容器包括最多的水。

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

原题比較直白的解释大概如此:数组中的每一个元素代表一个水槽的边。找出两条边。使得水槽的体积最大。

这里不用考虑宽度,仅仅要 底 x 高 最大就可以。即两个水槽边的距离要越远。边的高度要越大,可是依据“木桶原理”。此处由高度较小的边决定。

就可以由两边向中间搜索,找到使得面积最大的两条边。

	public int maxArea(int[] height) {
int area = 0;
int i = 0, j = height.length - 1;
while (i < j) {
area = Math.max(area, (j - i) * Math.min(height[i], height[j]));
if (height[i] > height[j])
j--;
else
i++;
}
return area;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

LeetCode——Container With Most Water的更多相关文章

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

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

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

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

  3. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  4. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  5. leetcode Container With Most Water

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

  6. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  7. [Leetcode] Container With Most Water ( C++)

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

  8. LeetCode Container With Most Water (Two Pointers)

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

  9. [Leetcode] container with most water 最大水容器

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

随机推荐

  1. Mac 终端提示You have not agreed to the Xcode license agreements

    在mac 终端运行命令的时候会被提示没有同意xcode 证书 ,这个时候需要在Terminal中同意license 打开终端输入xcodebuild -license,会看到一大堆的license说明 ...

  2. Python easy_insatll 安装包

    Python 使用 easy_instal安装第三方扩展包更容易 easy insatll 下载地址 https://pypi.python.org/pypi/setuptools/ windows ...

  3. ajax基础入门

    补充一下Ajax的使用方法 //可以复制下面两种方法在百度上实验 //jquery的使用方法 $.ajax({ url:"index.php", success:function( ...

  4. SQL server语句练习

    相关表: <span style="white-space:pre">create table DEPT ( <span style="white-sp ...

  5. 【翻译】十大要避免的Ext JS开发方法

    原文地址:http://www.sencha.com/blog/top-10-ext-js-development-practices-to-avoid/ 作者:Sean Lanktree Sean ...

  6. mfc menu用法一

    #define IDM_NEW 0x1002#define IDM_EXIT 0x1003#define IDM_HELP 0x1004#define IDM_ABOUT 0x1005 hMenu = ...

  7. CImage类

    CImage封装了DIB(设备无关位图)的功能,因而可以让我们能够处理每个位图像素.这里介绍GDI+和CImage的一般使用方法和技巧. TAG: GDI  CImage  后处理   我们知道,Vi ...

  8. OCM读书笔记(2) - PL/SQL 基础

    1. % type 用法,提取% type所在字段的类型 declare     myid dept.deptno % type;    myname dept.dname % type;begin  ...

  9. Github上四种Lisp方言的流行度 | 肉山博客 (Wenshan's Blog)

    Github上四种Lisp方言的流行度 | 肉山博客 (Wenshan's Blog) Github上四种Lisp方言的流行度

  10. exe解析

    IMAGE_DOS_HEADER      size   0x40 e_lfanew                           0xe0 当中有0xA0的间隔数据. IMAGE_NT_HEA ...