题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

  For example, 
  Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

例如给出上图中的黑色部分(数组表示),让你求出蓝色部分。

这也是个神题。。。当然对小白我来说。

想了半天,是不是遍历数组呢,然后依次计算当前bar构成的container大小。问题在于,这个方法要考虑的边界条件太多了。写了一个很复杂的算法,还用到了stack来记录碎片的蓝色部分,结果最后还是miss很多情况。

后来想了一个复杂度比较高的但是代码看来了比较简单的算法。觉得简直要被BS屎了。但是还是记录下来以村托牛人的牛。

想的方法我取名叫俄罗斯方块法,为啥,你马上就晓得鸟。

把上面的图图,instead of看成一个个柱子,我看成一排排的方块。

然后,对每一行,我数数有多少蓝色的方块,累加起来,不就是总共的蓝色部分了么。

这个复杂度有点高,取决与最大的元素的值*O(n)。

解法一:

 public static int trap(int[] A){
int i = 0;
int j = A.length - 1;
int sumArea = 0;
while(i + 1 < j){
//首先找到两边都不是0的位置
while(A[i] <= 0 && i < j)i++;
while(A[j] <= 0 && i < j)j--;
//然后数数当前行有多少蓝色方块,也是就0的个数啦。
//同时记录最小值
int min = Integer.MAX_VALUE;
for(int k = i; k < j;k++){
if(A[k] == 0) sumArea += 1;
if(A[k] < min) min = A[k];
}
//为记录下一行做准备,消除俄罗斯方块。。。。
int step = Math.max(min, 1);
for(int k = i; k <= j; k++){
if(A[k] > 0)A[k]-= step;
}
}
return sumArea;
}

然后大集合就可耻地潮湿了。

leetcode的牛人是怎么做的呢?人家不仅有O(n)的解法,而且constant space。膜拜。

首先,找到最高的一个柱子,例如例子中的3。

然后遍历左半边。从i=0开始靠近mid,遍历过程中维护一个curHeight,表示这当前遇到的最大的height值;如果当前的bar的height小于最大值,那么当前的bar能贡献的water就是height - curMaxHeight。否则的话,更新curMaxHeight。

为了更好理解这个算法,我们来track下这个过程:

     

         

最后遍历右半边。过程是一模一样的,只不过i从最右边靠近Mid。

解法二

代码如下:

 public static int trap2(int[] A){
if(A.length <= 1) return 0;
int curMaxHeight = 0;
int water = 0;
int mid = 0;
for(int i = 0; i < A.length;i++){
if(A[i] > A[mid]) mid = i;
} for(int i = 0; i < mid; i++){
if(A[i] < curMaxHeight){
water += curMaxHeight - A[i];
}else curMaxHeight = A[i];
} curMaxHeight = 0;
for(int i = A.length - 1; i > mid; i--){
if(A[i] < curMaxHeight){
water += curMaxHeight -A[i];
}else curMaxHeight = A[i];
}
return water;
}

其实,本质上来说,第一步保障了左右两边的水总是能“放进去”,因为大板子在中间档着嘛。

Faint,我写到这里,想到这个代码其实也是蛮简单的,为啥我就没想到呢!唉。。。

总结下:

复杂的代码是错误的代码。没有例外。

LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]的更多相关文章

  1. leetcode 第41题 Trapping Rain Water

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  2. LeetCode(42)Trapping Rain Water

    题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, com ...

  3. LeetCode: Trapping Rain Water 解题报告

    https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...

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

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

  5. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. [Leetcode][Python]42: Trapping Rain Water

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 42: Trapping Rain Waterhttps://oj.leetc ...

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

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

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

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

  9. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

随机推荐

  1. 数据流图(DFD)画法

    数据流图(DFD)画法要求 一.数据流图(DFD) 1.数据流图的基本符号 数据流图由四种基本符号组成,见图5-4-1所示. 图5-4-1  数据流图的基本符号 例:图5-4-2是一个简单的数据流图, ...

  2. ARM开发工具软件命令具体解释---嵌入式回归第三篇

    先从bootloader開始,由于临时眼下这些都会是裸机程序相关. 本人这里是VMwarm10.0上安装的红帽linux虚拟机.从以下的截图中能够看出 裸机开发流程: 这里先做第三步(第一步第二步已提 ...

  3. mysql 开启慢查询 如何打开mysql的慢查询日志记录

    mysql慢查询日志对于跟踪有问题的查询非常有用,可以分析出当前程序里有很耗费资源的sql语句,那如何打开mysql的慢查询日志记录呢,接下来将详细为您介绍 原文出自:http://www.jbxue ...

  4. redhat yum替换成CentOS yum 并修改源

    wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm wge ...

  5. Struts2初学 Struts2的action接收用户数据方式

    一.简介    开发Web应用程序,首先应会遇到对用户输入数据的接收,传统的Web应用程序是由开发人员调用HttpServletRequest的getparameter(String name)方法从 ...

  6. PyCharm 在django程序中单独运行py文件

    使用PyCharm开发django程序,发现如果不在命令行而在IDE的django项目中直接运行django程序,发现报错,程序如下:   def main(): from people.models ...

  7. 拿与不拿的dfs

    在n个物品中拿k个,使得花费恰好为m. 典型的dfs,对每一个物品,可以选择拿与不拿,然后在判断下一个物品. 失败的dfs: 代码没有保存,只重写一下dfs函数的关键部分: ;i<n;i++) ...

  8. Caliburn Micro框架快速上手(WP)

    一.使用nuget添加起始工程         二.修改App.xaml文件和App.xaml.cs文件     AppBootstrapper介绍: AppBootstrapper根据中文的直译可以 ...

  9. Excel关闭事件

    记录一下,弄VBA曾经遇到一个需求,遇到用到这个事件,找了很久,最后还是问别人才知道的. Sub Auto_Close() ThisWorkbook.Saved = True End Sub

  10. [i.MX6q]i.MX6q处理器,linux操作系统平台搭建 从SD卡启动系统

    转自:http://www.07net01.com/linux/2016/02/1232094.html 参照1:http://blog.csdn.net/girlkoo/article/detail ...