[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.

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

 

目录
相关文章
|
6月前
Leetcode 365. Water and Jug Problem
一句话理解题意:有容积为x和y升的俩水壶,能不能量出z升的水。 我刚开始看到这题,立马就想了下暴力搜索的可能性,但考虑了下数据大小,立马放弃这个暴力的想法,于是意识到肯定有比较简单的数学方法,其实我自己没想到,后来看还是看了别人的代码,很多博客都直接给出了解法, 但没介绍为什么能这么解。所以我决定解释下我自己的思路。
29 0
LeetCode 417. Pacific Atlantic Water Flow
给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。 规定水流只能按照上、下、左、右四个方向流动,且只能从高到低或者在同等高度上流动。 请找出那些水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标。
79 0
LeetCode 417. Pacific Atlantic Water Flow
LeetCode 407. Trapping Rain Water II
我们把最外面的四边当成四面墙,想象海水面不断的升高,首先会浸过墙面最低的格子,如果墙面最低格子的四周(出了在墙面的格子)有比它矮的格子,那么这就可以形成一个蓄水池,蓄水池的最高高度就是墙面最低的格子,于是我们计算这个蓄水池可以获得的蓄水量;然后这个蓄水池被添加到墙面中;继续在墙面中找最低的格子;
61 0
LeetCode 407. Trapping Rain Water II
|
索引
LeetCode 42 Trapping Rain Water
给定n个非负整数,每个非负整数表示一个宽度为1的柱子,计算下雨后能够捕获多少水.
49 0
LeetCode 42 Trapping Rain Water
|
机器学习/深度学习 PHP 索引
[Leetcode 之 PHP 解析 (42. Trapping Rain Water)
[Leetcode 之 PHP 解析 (42. Trapping Rain Water)
87 0
[Leetcode 之 PHP 解析 (42. Trapping Rain Water)
LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water  Problem's Link  ---------------------------------------------------------------------------- Mean:  在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体.
878 0
|
6天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
6天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
13 0