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. 3、MyBatis.Net学习笔记之增删改

    增删改之前先说一下笔记1里提到的一个无法创建ISqlMapper对象的问题. <resultMaps> <resultMap id="FullResultMap" ...

  2. 转:gpio_direction_output 与 gpio_set_value

    gpio_set_value(port_num,0/1) 一般只是在这个GPIO口的寄存器上写上某个值,至于这个端口是否设置为输出,它就管不了! 而gpio_direction_output (por ...

  3. RocksDB介绍:一个比LevelDB更彪悍的引擎

    关于LevelDB的资料网上还是比较丰富的,如果你尚未听说过LevelDB,那请稍微预习一下,因为RocksDB实际上是在LevelDB之上做的改进.本文主要侧重在架构上对RocksDB对LevelD ...

  4. 工作那些事(二)应聘时填写个人信息ABCD

    先看看都有那些: 公司A: 填写来访人员登记表(在前台的那种),内容包括: 姓名.时间.电话.职位. 公司B: 填写来访人员登记表(在前台的那种),内容包括: 姓名.时间.电话.身份证号码().事由( ...

  5. wikioi3363支线剧情(挖个坑,不会做。。。)

    3363 支线剧情 题目描述 Description 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往都有很多 ...

  6. datagridview bindingsource刷新数据

    调用bindindsource的ResetBindings() 方法

  7. WCF扩展系列 - 行为扩展(Behaviors)

    原文地址:http://www.cnblogs.com/Creator/archive/2011/05/21/2052687.html 这个系列的第一部分将会重点关注WCF行为(behaviors), ...

  8. poj1006

    题目大意:生物节律 一些人相信有三种循环在一个人生命中从他或者她出生的那天起,这个三个周期是,身体,情绪,智力, 并且他们有23,28,和33天的时间,在每一个周期里面都有一个周期,在一个周期的高峰期 ...

  9. Cstyle的UEFI导读之SEC第一篇 Reset Vector

        最近小看了一下SEC部分的code,现在来做个总结.所谓SEC就是CPU刚刚完成硬件初始化的是时候执行的和CPU体系架构息息相关的代码.主要是为后续CPU以及Chipset初始化代码所需的必备 ...

  10. HDU 4118 树形DP Holiday's Accommodation

    题目链接:  HDU 4118 Holiday's Accommodation 分析: 可以知道每条边要走的次数刚好的是这条边两端的点数的最小值的两倍. 代码: #include<iostrea ...