题目:在二维坐标系下,有很多个挡板,有两个挡板之间能够积蓄的水的最大面积。如下图所示:

思路:我只想到暴力解法,用O(n2)的时间复杂度算出任意两个挡板形成的面积,这必须的过不了。

优化解法:O(n).

用两个指针 i 和 j 指向整个height[]数组的头尾。

if i 指向的高度 < j 指向的高度 , then 用 i 指向的高度 * i , j之间的距离 求的面积,i 指针向后移。

(i指针向后移的原因:因为当前的面积由i 的高度决定,相当于现在获得以i指针为一边挡板的最大面积,因为随着j的向内移动,水平宽度肯定是降低的,然后及时j的高度高于i的高度,也因为矩形面积由短板边<i的高度>决定,因此不会比现在的面积更大。)

同理如果 j 指向的高度 < i 指向的高度, 将j指针向内移动。

代码:

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

这种精妙的算法,代码很简单,思路很难想到。即使知道了正确解法,还是觉得心有不安。待下次遇见类似问题再深化思考吧。

[leetcode]_Container With Most Water的更多相关文章

  1. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))

    LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...

  4. [LeetCode] 407. Trapping Rain Water II 收集雨水 II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  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, 解题报告

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

  7. [leetcode][042] Trapping Rain Water (Java)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...

  8. [LeetCode] Swim in Rising Water 在上升的水中游泳

    On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j). Now rain star ...

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

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

随机推荐

  1. Linux文件夹文件创建、删除

    Linux删除文件夹命令 linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm ...

  2. C++学习19 类的多继承

    在前面的例子中,派生类都只有一个基类,称为单继承.除此之外,C++也支持多继承,即一个派生类可以有两个或多个基类. 多继承容易让代码逻辑复杂.思路混乱,一直备受争议,中小型项目中较少使用,后来的 Ja ...

  3. java利用过滤器实现编码的转换,内容输出的替换

    在页面建个表单 <form action="login.do" method="post"> <input type="text&q ...

  4. SecureCRT的背景和文字颜色的修改

    options->;session options->;emulation->;terminal选择linux(相应的服务器系统)ansi color 打上钩options-> ...

  5. C Primer Plus(第五版)7

    第 7 章 C 控制语句:分支和跳转 在本章中你将学习下列内容: · 关键字:if(如果),else(否则),switch(切换),continue(继续),break(中断), case(情况),d ...

  6. ZOJ 3329 【概率DP】

    题意: 给你三个均匀k面筛子. 分别有k1 k2 k3个面,每个面朝上的概率是相等的. 如果第一个筛子出现a第二个筛子出现b第三个筛子出现c那么置零. 否则在当前和加上三个点数之和. 求当前和大于n需 ...

  7. 错误记录,找不到sqlite dll

    Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies.找不到指定模块. 在CSDN找到解决 ...

  8. 学习Webservice之入天气小试

    主要方法是:通过程序中设置代理用公司内网访问外部Webservice public InputStream getSoapInputStream(String url) { InputStream i ...

  9. ua实现类似 www.taobao.com 在手机上打开自动转变为 m.taobao.com 的域名转换

    user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.25 ...

  10. java.lang.Exception: Socket bind failed: [730013] An attempt was made to acc

    在CMD命令行中启动运行startup.bat,启运程序总是闪退,查看日志发现如下错误: 26-Jan-2016 18:12:34.463 SEVERE [main] org.apache.coyot ...