C#解leetcode 11. Container With Most Water
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.
由于这个题的题意十分不清楚,有必要解释一下:这可以理解为在一个二维坐标系下面,求两条与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的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ... 
- Leetcode  11.  Container With Most Water(逼近法)
		11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ... 
- 如何装最多的水? — leetcode 11. Container With Most Water
		炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ... 
- [LeetCode] 11. Container With Most Water 装最多水的容器
		Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ... 
- LeetCode 11. Container With Most Water (装最多水的容器)
		Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ... 
- [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). ... 
- LeetCode#11. Container With Most Water
		问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ... 
- Java [leetcode 11] Container With Most Water
		问题描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ... 
- [leetcode]11. Container With Most Water存水最多的容器
		Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ... 
随机推荐
- Redmine配置
			官网步骤说明 http://www.redmine.org/projects/redmine/wiki/RedmineInstall 搭环境 1.MySql 2.RailsInstaller:Redm ... 
- Zephyr-开发流程
			开发流程 前提1:检查你的Linux主机满足入门指南中规定的最低要求. 具体请参考 : 物联网操作系统-Zephyr 前提2: 确保SDK的环境变量和zephyr项目的环境变量. 终端执行: $ e ... 
- jQuery--Promise object
			http://blog.mediumequalsmessage.com/promise-deferred-objects-in-javascript-pt2-practical-use http:// ... 
- cf B  Bear and Strings
			题意:给你一个字符串,然后找多少区间内含有“bear”,输出数目: #include <cstdio> #include <cstring> #include <algo ... 
- (转载)用PHP正则表达式清除字符串的空白
			(转载)http://www.chinaz.com/program/2009/0220/67569.shtml 我们经常会处理来自用户输入或从数据库中读取的数据,可能在你的字符串中有多余的空白或制表符 ... 
- 【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)
			题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804 题目大意: 一个有向无环图(DAG),有N个点M条有向边(N,M<=105 ... 
- win7使用USB转串口连接mini2440方法
			不想嚼别人吃剩的馍.我只说我自己是怎么痛苦的连上的. 环境设备: window7笔记本,没有串口,装有XP和Ubuntu2个虚拟机(不是必须的,我只是说明一下我的环境) 友善之臂mini2440(含U ... 
- [置顶] C++中RTTI机制剖析
			C++中要想在运行时获取类型信息,可没有Java中那么方便,Java中任何一个类都可以通过反射机制来获取类的基本信息(接口.父类.方法.属性.Annotation等),而且Java中还提供了一个关键字 ... 
- multipath.conf
			# This is a basic configuration file with some examples, for device mapper# multipath.# For a comple ... 
- Excel2003读取sqlserver数据库表数据(图)
