Trapping Rain Water (Bar Height) -- LeetCode

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 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!

思路:使用两个指针left和right,分别从两端向中间靠拢。同时,记录left和right曾经到过的最高值。如果当前值并没有超过最高值,则将最高值与当前的差值作为水的量,并将指针加一(right指针是减一)。为了让这个方法有效(无效的情况是,指针后续遇见的bar都不会再高于最高值了,那么这些水根本存不起来),因此我们每次只移动left和right中矮的那一个(如果两者相等,则移动left)。这样,一定能保证最后会碰到比最高值要高的bar(至少是等高),因为我们移动的是矮的那个指针,往前移动肯定最后会遇见另一个高的指针。

 1 class Solution {
 2 public:
 3     int trap(vector<int>& height) {
 4         if (height.size() == 0) return 0;
 5         int left = 0, right = height.size() - 1, res = 0;
 6         int maxleft = height[left], maxright = height[right];
 7         while (left < right)
 8         {
 9             if (height[left] <= height[right])
10             {
11                 if (height[left] > maxleft) maxleft = height[left];
12                 else res += maxleft - height[left];
13                 left++;
14             }
15             else
16             {
17                 if (height[right] > maxright) maxright = height[right];
18                 else res += maxright - height[right];
19                 right--;
20             }
21         }
22         return res;
23     }
24 };

bar height问题:这是Amazon面试中的一道问题。求最高的液面高度。这里只需要把上方的代码修改一下就可以用。就是在res值变更时,若max与当前高度差值非零时,记录下max值,最后最高的max值就是结果。

 1 class Solution {
 2 public:
 3     int trap(vector<int>& height) {
 4         if (height.size() == 0) return 0;
 5         int left = 0, right = height.size() - 1, res = 0;
 6         int maxleft = height[left], maxright = height[right];
 7         while (left < right)
 8         {
 9             if (height[left] <= height[right])
10             {
11                 if (height[left] > maxleft) maxleft = height[left];
12                 else if (maxleft - height[left] > 0)
13                     res = max(res, maxleft);
14                 left++;
15             }
16             else
17             {
18                 if (height[right] > maxright) maxright = height[right];
19                 else if (maxright - height[right] > 0)
20                     res = max(res, maxright);
21                 right--;
22             }
23         }
24         return res;
25     }
26 };
时间: 2024-12-28 20:17:37

Trapping Rain Water (Bar Height) -- LeetCode的相关文章

[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

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

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

leetcode之Container With Most Water 和Trapping Rain Water

Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together wi

leetCode 42.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