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

思路:我只想到暴力解法,用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. 使用mustache.js 模板引擎输出html

    看了https://mustache.github.io/你就知道mustache是非常强大的模板引擎,支持多种语言,下面是个简单入门例子: MVC Model public class Studen ...

  2. oracle 游标笔记

    declare v_x number; v_y number; v_geo clob; cursor cur is select c_x, c_y from t_map_data where c_ty ...

  3. Django – query not equal

    The simpliest way to retrieve data from tables is take them all. To do this,  you can write: 1 all_e ...

  4. Spark-1.5.2安装

    1.下载scala-2.10.6包解压到指定目录 #SCALA VARIABLES START export SCALA_HOME=/usr/local/scala-2.10.6 export PAT ...

  5. Android——TableLayout

    TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行. TableLayout的列数等于含有最多子控件的TableRow的列数.如第一Table ...

  6. 在解决方案中所使用 NuGet 管理软件包依赖

    使用程序包恢复功能可以在提交源代码时, 不需要将代码库提交到源代码管理中,大幅减少项目的尺寸.所有NuGet程序包都存储在解决方案的Packages文件夹中. 要启用程序包恢复功能,可右键单击解决方案 ...

  7. Linux下查看文件和文件夹大小(转)

    觉得挺有用的,就转到了自己的博客里,方便以后查询: 转自:http://www.cnblogs.com/benio/archive/2010/10/13/1849946.html 当磁盘大小超过标准时 ...

  8. pytest学习笔记(三)

    接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...

  9. Orchard官方文档翻译(四) 让Orchard在WebMatrix下工作

    原文地址:http://docs.orchardproject.net/Documentation/Working-with-Orchard-in-WebMatrix 想要查看文档目录请用力点击这里 ...

  10. Datable 排序

    if(dt.Columns.IndexOf("name") != -1) //存在这个字段 { dt.DefaultView.Sort = "name asc" ...