class Solution:
def calLeft(self,height,rightval,left,right):
if left>=right:
return 0
sums = 0
maxindex=0
maxval=-1
for i in range(left,right):
cur = height[i]
if cur>maxval:
maxval=cur
maxindex=i
waterlevel = min(maxval,rightval)
for i in range(maxindex+1,right):
sums += max(0,waterlevel-height[i])
sums += self.calLeft(height,waterlevel,0,maxindex)
return sums
def calRight(self,height,leftval,left,right):
if left>=right:
return 0
sums = 0
maxindex = 0
maxval = -1
for i in range(right,left,-1):
cur = height[i]
if cur>maxval:
maxval=cur
maxindex = i
waterlevel = min(maxval,leftval)
for i in range(left+1,maxindex):
sums += max(0,waterlevel-height[i])
sums +=self.calRight(height,waterlevel,maxindex,len(height)-1)
return sums def trap(self, height: 'List[int]') -> int:
maxindex = 0
maxval = -1
sums = 0
for i in range(len(height)):
curval = height[i]
if curval > maxval:
maxval = curval
maxindex = i
sums += self.calLeft(height,maxval,0,maxindex)
sums += self.calRight(height,maxval,maxindex,len(height)-1)
return sums

这道题的主要思想是搜索,先找到最大的值maxval,对应的索引maxindex,然后向左右两个方向分别搜索。记height的长度为length。

左侧的部分[0,maxindex),从小到大寻找当前区间的最大值leftmaxval,对应的索引leftmaxindex,然后从计算(leftmax,maxindex)之间的区域。

用递归的方式,继续计算[0,leftmaxindex),一直到这个区间长度为0,不再进行递归。

同理,右侧部分(maxindex,length],从大到小寻找最大值rightmaxval,对应的索引rightmaxindex,然后计算(midindex,rightmaxindex)之间的区域。

用递归的方式,继续计算(rightmaxindex,length],一直到这个区间的长度为0,不再进行递归。

在主函数中,找到全局的(第一个出现的,也可以是任意一个)最大值,然后分别调用左右两侧的递归方法。

这里有个小技巧(贪心思想),就是左侧区间找最靠左的最大值,右侧区找最靠右的最大值。这样能尽可能扩大每次搜索所确定的区域,使得在每次搜索到多个相等的最大值时,可以减少递归次数。

本题还有其他的解决方案,使用非递归的方式效率更高。

leetcode42的更多相关文章

  1. LeetCode42 Trapping Rain Water

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

  2. [Swift]LeetCode42. 接雨水 | Trapping Rain Water

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

  3. Leetcode42. 接雨水

    42. 接雨水 做法 考虑单独一列能生产多少贡献:用左右最大值去接这列的水 \(O(n)\) Code class Solution { public: int mx[1000000],rx[1000 ...

  4. LeetCode42题,单调栈、构造法、two pointers,这道Hard题的解法这么多?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第23篇文章. 今天来看一道很有意思的题,它的难度是Hard,并且有许多种解法. 首先我们来看题面,说是我们有若 ...

  5. LeetCode---42. 接雨水 (hard)

    题目:42. 接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 示例: 输入:height = [0,1,0,2,1,0,1,3,2,1,2, ...

  6. LeetCode42. 接雨水(java)

    42.接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种 ...

  7. 2018Java开发面经(持续更新)

    不要给自己挖坑!!!不要给自己挖坑!!!不要给自己挖坑!!!如果面试官只是问你了解xxx吗,如果不是很了解,就直接说不知道,不要说知道,不然面试官深问再不知道就印象很不好! 处女面送给了头条(北京)日 ...

  8. 2018java开发一些面经

    算法系列:https://www.cnblogs.com/yanmk/p/9232908.html 2018Java开发面经(持续更新) 不要给自己挖坑!!!不要给自己挖坑!!!不要给自己挖坑!!!如 ...

  9. 单调队列 && 单调栈

    单调队列 && 单调栈 单调队列 维护某个滑动区间的min or max,可用于dp的优化 以维护min为例,采用STL双端队列实现 每次加入元素x前 先检查队首元素==滑动后要删除的 ...

随机推荐

  1. c++ 生成dll文件并调用

    2 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

  2. TestLink测试管理工具的使用举例—第二篇

    本篇博客接上面TestLink测试管理工具的使用举例—第一篇的内容继续讲解如何使用TestLink工具进行测试管理. 创建一个名为“购物V1.1系统测试”的测试计划. 2.2版本管理 点击主页“测试计 ...

  3. 剖析 GSM 加密机制以及位置更新的过程

    你有没有想过打开手机时会发生什么?它是如何以安全的方式与网络进行通信?几乎所有人都知道TCP / IP,并且可能许多人还是专家,但是谈到电信方面,很少有人知道它的内部原理. gsm中的消息结构是什么? ...

  4. JS两个页面通过URL传值

    1.传递参数: window.location.href = "./list.html?id="+id; 1.接收参数: (1)接收参数函数封装 function GetReque ...

  5. 第一次学习手游开发:Flappy Bird制作

    周末有空,学习下手游开发,因为本人压根没有基础,于是找了个视频,跟着教程撸了个Flappy Bird. 感谢 SwiftV课堂 提供的教学视频 源码地址: https://github.com/phe ...

  6. 新建一个self hosted Owin+ SignalR Project(1)

    OWIN是Open Web Server Interface for .Net 的首字母缩写,他的定义如下: OWIN在.NET Web Server 与Web Application之间定义了一套标 ...

  7. Semantic Compositionality through Recursive Matrix-Vector Spaces-paper

    Semantic Compositionality through Recursive Matrix-Vector Spaces 作者信息:Richard Socher Brody Huval Chr ...

  8. hibernate模拟(转载)

    package simulation; /** * * @author Administrator * */ public class User { private int id; private S ...

  9. poj2279——Mr. Young's Picture Permutations

    Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...

  10. Bootstrap格式转换代码

    网址:http://www.w3cschool.cc/bootstrap/bootstrap-responsive-utilities.html <div class="contain ...