Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

由于这个题的题意十分不清楚,有必要解释一下:这可以理解为在一个二维坐标系下面,求两条与x垂直的直线 和 x轴 围成的  图形的面积最大值。

在讨论区看到了一个解法非常巧妙的回答,姑且摘录如下。如要更好的理解这个算法的思想,有必要看看下面的解释说明:

首先假设有一个6*6的矩阵,如图。在图中x的部分代表不用计算的情况,之所以不用计算是因为:

(1)对角线上两个元素相等

(2)左下三角和右上三角式对称的,只需要计算其中之一就行,我们选择右上三角进行计算

我们首先计算(1,6)点(可以理解为取第一条直线和第二条直线),标记为o。如果左侧的直线小于右侧的直线,则(1,2),(1,3),(1,4),(1,5)的值都会比(1,6)要小(因为计算面积的时候取得是两条直线中的较小值,所以其余的组合一定小于(1,6)),因此,其余的组合都可以不用在计算了。不用计算的点用---表示。

然后我们移动到了(2,6)点(也就是选择第二条直线和第六条直线),此时,如果右侧的直线小于左侧的直线,则(3,6),(4,6),(5,6)又可以不用计算了

按照上面的规律,我们不论o最终移动到什么地方,我们仅仅需要进行n-1次判断就可以得到结果

下面贴上用C#语言的实现过程:

public class Solution {
public int MaxArea(int[] height) { int max=,Area=,i=,j=height.Length-; while(i!=j)
{
if(height[i]<height[j])
{
Area= height[i]*(j-i);
i++;
}
else
{
Area= height[j]*(j-i);
j--;
}
max=Math.Max(max,Area);
} return max;
}
}

C#解leetcode 11. Container With Most Water的更多相关文章

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

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

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

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

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

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

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

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

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

  6. [LeetCode] 11. Container With Most Water My Submissions Question 解题思路

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

  7. LeetCode#11. Container With Most Water

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

  8. Java [leetcode 11] Container With Most Water

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

  9. [leetcode]11. Container With Most Water存水最多的容器

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

随机推荐

  1. 《C和指针》章节后编程练习解答参考——6.1

    <C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...

  2. Linux查看端口号

    查看端口占用情况的命令:lsof -i 查看某一端口的占用情况: lsof -i:端口号 结束占用端口的进程:killall 进程名 也可使用命令: netstat -apn|grep <端口号 ...

  3. EJB

    Enterprise JavaBean,企业级javabean,是J2EE的一部分,定义了一个用于   开发基于组件的企业多重应用程序的标准.其特点包括网络服务支持和核心开发工具(SDK). 是Jav ...

  4. 优秀开源项目的svn地址

    很多优秀的开源项目已经提供SVN源码签出了,无论是解疑还是学习,都是一大幸福之事啊! Apache的SVN库,强烈推荐! http://svn.apache.org/repos/asf/ 里面不但有S ...

  5. Node.js事件循环

    Node JS是单线程应用程序,但它通过事件和回调概念,支持并发. 由于Node JS每一个API是异步的,作为一个单独的线程,它使用异步函数调用,以保持并发性.Node JS使用观察者模式.Node ...

  6. Node.js教程

    Node.js是什么? Node.js是建立在谷歌Chrome的JavaScript引擎(V8引擎)的服务器端平台.Node.js是由瑞恩·达尔在2009年开发的,它的最新版本是v0.10.36. N ...

  7. 初始化rails上的compass项目

    compass以外还有一个很实用的scss模块, _media-queries.scss 通过终端下载 curl -O https://raw.github.com/paranoida/sass-me ...

  8. Linux&shell之Shell脚本

    写在前面:案例.常用.归类.解释说明.(By Jim) 使用多条命令shell脚本的关键是可以输入多条命令,甚至可以将一条命令的结果传递给另一条命令.date;who(两个命令shell脚本的写法) ...

  9. 关于SVN的操作批处理示例

    关于SVN的操作批处理示例 为了一句话:不要动手做机器能够做的事情. 天天工作用svn,更新啥的打开目录啥的动作天天在重复.每次写些命令也蛮无聊的,不说了,看下面: 1 @echo off 2 rem ...

  10. 5451 HDU Best Solver

    链接: Best Solver 题目分析: 这个题目的关键点是需知道“共轭”. 如 :(A√B + C√D)  和 (A√B - C√D) 是共轭的 这个有一个规律 (A√B + C√D)^n + ( ...