[LeetCode]题解(python):042-Trapping Rain Water

题目来源:

  https://leetcode.com/problems/trapping-rain-water/



题意分析:

  输入一组数组,代表一个宽度为1的高度地图。问,这个地图在雨后可以收集多少水。例如,输入一个数组[0,1,0,2,1,0,1,3,2,1,2,1],返回的是6.如图所示:



题目思路:

  这道题目虽然说是hard难度的题目,但是其实不是很难。不难发现,水都是从最高那个数起和第二高数之间。那么这题可以分成两部。①找到数组的最大值。②计算最大值左边和右边分别可以收集多少水;计算左边的时候,首先找到左边最靠左的高度,计算它和最高数之间收集的水和第二高往左的和,形式化表示就是f(i) = w(i,j) + f(j),f(i)代表从i往左收集的水,w(i,j)代表i和j之间收集的水。w(i,j)和容易算,就是i到j之间第二高减去原来高度之和;右边计算类似。时间复杂度是(O(n))。



代码(python):

  

 1 class Solution(object):
 2     def left(self,height,i):
 3         if i == 0:
 4             return 0
 5         n = i - 1;mi = n;maxh = height[mi]
 6         while n >= 0:
 7             if height[n] >= maxh:
 8                 maxh = height[n];mi = n
 9             n -= 1
10         ans = 0;k = mi
11         while k < i:
12             ans += maxh - height[k]
13             k += 1
14         return ans + self.left(height,mi)
15     def right(self,height,i):
16         size = len(height)
17         if i == size - 1:
18             return 0
19         n = i + 1; mi = n;maxh = height[mi]
20         while n < size:
21             if height[n] >= maxh:
22                 maxh = height[n];mi = n
23             n += 1
24         ans = 0;k = mi
25         while k > i:
26             ans += maxh - height[k];k -=1
27         return ans + self.right(height,mi)
28     def trap(self, height):
29         """
30         :type height: List[int]
31         :rtype: int
32         """
33         s = len(height)
34         if s == 0:
35             return 0
36         mh = height[0];m = 0; i = 1
37         while i < s:
38             if height[i] >= mh:
39                 mh = height[i];m = i
40             i += 1
41         return self.left(height,m) + self.right(height,m)
42         



转载请注明出处:http://www.cnblogs.com/chruny/p/4934365.html

时间: 2024-11-05 21:35:29

[LeetCode]题解(python):042-Trapping Rain Water的相关文章

LeetCode 042 Trapping Rain Water

题目要求:Trapping Rain Water 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. The above elevatio

[Leetcode][Python]42: Trapping Rain Water

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 42: Trapping Rain Waterhttps://oj.leetcode.com/problems/trapping-rain-water/ Given n non-negative integers representing an elevation map where the width of each bar is 1,compute how much water

【LeetCode】042 Trapping Rain Water

题目: 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. The above elevation map is represented

leetcode 第41题 Trapping Rain Water

题目: 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. The above elevation map is represented

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

题目在这里: https://leetcode.com/problems/trapping-rain-water/ [标签] Array; Stack; Two Pointers 这个题我感觉很难,我自己是完全不会.下面贴的是别人想到的好方法,自己适当做了些简单调整. 我觉得,这个解法非常巧妙的使用了双向的 two pointers.用到的变量有左右两个指针,一个在最左边,一个在最右边,然后两个一起往中间走.在走的时候维护两个变量,left barrier 和 right barrier,即左边

Java for LeetCode 042 Trapping Rain Water

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. 解题思路: 先找到第一块最高的木板,然后从前向最高木板遍历,接着从后向最高木板遍历,J

042 Trapping Rain Water

这道题有一个直观的想法, 就是分别记录每个点的左侧和右侧最大值 对于height[i] 这一点能装的水等于 min(leftMax[i], rightMax[i]) - height[i]. 这个解法需要扫描2次序列. 以下的方法只需要扫描一次序列即可. class Solution: # @param {integer[]} height # @return {integer} def trap(self, height): stack = [] ans = 0 i, length = 0,

LeetCode: Trapping Rain Water 解题报告

https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven 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,

[leetcode]Trapping Rain Water @ Python

原题地址:https://oj.leetcode.com/problems/trapping-rain-water/ 题意: 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,

Trapping Rain Water leetcode java

题目: 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. The above elevation map is represented