Trapping Rain Water 解答

Question

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.


The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

Solution 1 -- DP

关键是将问题转换成求和,考虑每一个格子的可以贡献的水量。如上图所示,每一个格子上可以储存的最大水量由它左右两边决定。

volumn = min(max_left, max_right) - height

所以我们可以用DP来算出每个格子的max_left和max_right,进而可以得到总的水量。

 1 class Solution:
 2     def trap(self, height: List[int]) -> int:
 3         if not height:
 4             return 0
 5         L = len(height)
 6         dp_right = [height[0]] + [0] * (L - 1)
 7         dp_left = [0] * (L - 1) + [height[L - 1]]
 8         result = 0
 9         for i in range(1, L):
10             dp_left[i] = max(dp_left[i - 1], height[i - 1])
11         for i in range(L - 2, 0, -1):
12             dp_right[i] = max(dp_right[i + 1], height[i + 1])
13             tmp = min(dp_left[i], dp_right[i]) - height[i]
14             if tmp > 0:
15                 result += tmp
16         return result

Solution 2 -- Two Pointers

双指针法,一次遍历。

 1 class Solution:
 2     def trap(self, height: List[int]) -> int:
 3         if not height:
 4             return 0
 5         l, r = 0, len(height) - 1
 6         result = 0
 7         while l < r:
 8             tmp = min(height[l], height[r])
 9             if height[l] < height[r]:
10                 l += 1
11                 while l < r and height[l] < tmp:
12                     result += tmp - height[l]
13                     l += 1
14             else:
15                 r -= 1
16                 while l < r and height[r] < tmp:
17                     result += tmp - height[r]
18                     r -= 1
19         return result

原文地址:https://www.cnblogs.com/ireneyanglan/p/11517965.html

时间: 2024-10-22 17:36:00

Trapping Rain Water 解答的相关文章

有意思的数学题:Trapping Rain Water

LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的部分 假设:积木宽度均为1 输入:各个积木的高度 输出:所有积木能容纳水的“面积” 思考过程 1. 逐一求积木的间隔似乎不太容易.特别对于图中3-7积木间的容积,如果可以先求底部(4-6间)的容积,当求解上层(3-7)的容积时,还需要做额外的处理,如减掉底部的高度. 2. 既然如此,可否先求出3-7

[LeetCode][JavaScript]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 elevation map

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

Leetcode 407. Trapping Rain Water II

Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to trap after raining. Note:Both m and n are less than 110. The height of each unit cell is greater tha

Trapping Rain water 诸多细节须注意

Trapping Rain Water Total Accepted: 35650 Total Submissions: 118319My Submissions Question Solution 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.

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]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,

Leetcode 动态规划 Trapping Rain Water

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Trapping Rain Water Total Accepted: 14568 Total Submissions: 50810My Submissions Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much wate

【leetcode】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 elevation map