[LeetCode] Trapping Rain Water

Note: The following idea is in fact from the last answer in this link, which leads to a clean code. I just reorganize it and add some explanations. I hope it is Ok.

The following are four solutions in C/C++/Java/Python respectively. The basic idea is that we set two pointers l and r to the left and right end of height. Then we get the minimum height (minHeight) of these pointers (similar to Container with Most Water due to the Leaking Bucket Effect) since the level of the water cannot be higher than it. Then we move the two pointers towards the center. If the coming level is less than minHeight, then it will hold some water. Fill the water until we meet some "barrier" (with height larger than minHeight) and update l and r to repeat this process in a new interval.



C

 1 int trap(int* height, int heightSize) {
 2     int l = 0, r = heightSize - 1, water = 0, minHeight = 0;
 3     while (l < r) {
 4         while (l < r && height[l] <= minHeight)
 5             water += minHeight - height[l++];
 6         while (r > l && height[r] <= minHeight)
 7             water += minHeight - height[r--];
 8         minHeight = height[l] <= height[r] ? height[l] : height[r];
 9     }
10     return water;
11 }


C++

 1 class Solution {
 2 public:
 3     int trap(vector<int>& height) {
 4         int n = height.size(), l = 0, r = n - 1, water = 0, minHeight = 0;
 5         while (l < r) {
 6             while (l < r && height[l] <= minHeight)
 7                 water += minHeight - height[l++];
 8             while (r > l && height[r] <= minHeight)
 9                 water += minHeight - height[r--];
10             minHeight = min(height[l], height[r]);
11         }
12         return water;
13     }
14 };


Java

public class Solution {
    public int trap(int[] height) {
        int n = height.length, l = 0, r = n - 1, water = 0, minHeight = 0;
        while (l < r) {
            while (l < r && height[l] <= minHeight)
                water += minHeight - height[l++];
            while (r > l && height[r] <= minHeight)
                water += minHeight - height[r--];
            minHeight = Math.min(height[l], height[r]);
        }
        return water;
    }
}


Python

 1 class Solution:
 2     # @param {integer[]} height
 3     # @return {integer}
 4     def trap(self, height):
 5         n = len(height)
 6         l, r, water, minHeight = 0, n - 1, 0, 0
 7         while l < r:
 8             while l < r and height[l] <= minHeight:
 9                 water += minHeight - height[l]
10                 l += 1
11             while r > l and height[r] <= minHeight:
12                 water += minHeight - height[r]
13                 r -= 1
14             minHeight = min(height[l], height[r])
15         return water
时间: 2024-10-12 02:11:34

[LeetCode] Trapping Rain Water的相关文章

[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 2012-03-10]

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 by a

LeetCode: Trapping Rain Water [041]

[题目] 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: 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 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 th

LeetCode——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 by a

Leetcode: 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 th

[LeetCode] 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 by a

leetcode Trapping Rain Water pthon

class Solution(object): def trap(self,nums): leftmosthigh = [0 for i in range(len(nums))] leftmax=0 for i in range(len(nums)): if nums[i] > leftmax: leftmax=nums[i] leftmosthigh[i] = leftmax print leftmosthigh sums=0 rightmax=0 for i in reversed(rang