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). ...
随机推荐
- 由lib引发的血案(opencv找不函数问题)
在使用opencv中的函数时,连续两次遇到函数找不到的问题,第一次查时按照他人说的包含进一个头文件后,果真还真解决了:然而第二次在调用cvInpaint函数时包含进对应头文件,编译通过但运行不成功还是 ...
- CentOS下命令行和桌面模式的切换方法(转载)
桌面模式和命令行模式的切换方法 用编辑器打开 /etc/inittab 文件(这里用的是vi,你可以选择你喜欢的): #vi /etc/inittab 打开效果图如下: 桌面模式 : 把光标所在 ...
- fragment 学习
fragment需要id是必须属性 <fragment android:id="@+id/frg1" android:name="com ...
- UIControl-IOS开发
UIControl-IOS开发 UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedControl分段控件.UISlider滑块.UITextField文本 ...
- Apache虚拟站点配置
简单虚拟站点配置: <VirtualHost 127.0.0.2:80> DocumentRoot E:/wamp/www/yue ServerName 127.0.0.2:80</ ...
- Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四)
Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四) 题记:本来计划的SolrCloud的Recovery策略的文章是3篇的,但是没想到Recovery的内容蛮多的,前面 ...
- jQuery常用特效插件汇总
jquery的CDN引用及下载地址 http://www.bootcdn.cn/jquery/ 1:semantictabs.js可以简单地制作Tabs菜单2:tabBox.js可以非常简单方便地 ...
- Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条
Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条 异步任务相信大家应该不会陌生,那么本章内容MOMO将带领大家学习Unity中的一些异步任务.在同步加载游戏场景的时候通常会使用方法 Ap ...
- Android 判断wifi和4G网络是否开启
public boolean isWifiAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) g ...
- HDU 1151 Air Raid(最小路径覆盖)
题目大意: 有n个城市,m条道路,城市的道路是单向. 现在我们的伞兵要降落在城市里,然后我门的伞兵要搜索所有道路.问我们最少占领多少个城市就可以搜索所有的道路了. 我们可以沿着道路向前走到达另一个城 ...