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.

For example, 
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

初看该题,想到是要找出所有的“凹槽”,在此基础上分开计算各部分存储体积;因此最关键的是“凹槽”的起点与终点界定

最终采用借助于栈找到合适的(起点:该高度下一个高度开始递减),如何界定终点?(终点:在出现有高度下降的点且下降的位置比左边界要高)

但是有几个问题需要注意:

1. 中间出现与左边界相同的高度时需要及时终止,分段计算,因为在计算片段时候结束条件是到达左边界值

2. 靠近尾部后如果没有严格出现合乎我们规定的“凹槽”,从尾向前一个个处理,并在中间值大于右部作为边界高度的时候进行替换

代码如下:

 class Solution {
 private:
     int contain;
     stack<int> record;
 public:
     int trap(vector<int>& height) {
         contain = 0;
         //keep every pair of concave shape
         int size = height.size();
         if (size == 0)
             return contain;
         int left, right;//record all the pairs
         record.push(height[0]);
         for (int i = 1; i<size;){
             //left-bound
             if (i< size&&height[i]<record.top()){
                 left = record.top();
                 record.push(height[i]);
             }
             else{
                 record.push(height[i]);
                 i++;
                 continue;
             }
             i++;
             while (i<size&&(height[i]>=record.top()||height[i]<record.top()&&record.top()<left)){
                 record.push(height[i]);
                 if (height[i++] == left)//handle  the same height concave shape
                     break;

             }
             //calculate the container
             int temp;//temp stores the temporary right bound
             if (i != size){
                 temp = record.top();//as the pre pair‘s right-bound and new pair‘s left-bound
                 record.pop();
             }
             else{//handle the last several elements
                 temp = record.top();
                 record.pop();
                 while (record.top()!=left&&temp < record.top()){
                     temp = record.top();
                     record.pop();
                 }
             }
             int refer = (left > temp ? temp : left);
             while (true){
                int h = record.top();
                record.pop();
                if (h == left)
                    break;
                if (refer > h)
                    contain += refer - h;
                else if (refer == temp)//短板位于右边
                    refer =temp= h;
            }
            record.push(temp);
         }
         return contain;
     }
 };

测试用例:

最简单的是示例图;

高度下降后未上升到到达左边界高度

下降过程中出现与左边界高度相同的高度

时间: 2024-10-14 04:22:35

Trapping Rain water 诸多细节须注意的相关文章

No.42 Trapping Rain Water

1 class Solution 2 { 3 public: 4 int trap(vector<int> &height) 5 {/* 6 雨水覆盖问题: 7 每一个bar能承受的水量为:它左侧所有中最高的和右侧所有中最高的中取最小值作为一个瓶颈[否则也留不住], 8 若该值大于当前bar的高度,其差值即为所求 9 累加所有bar能承受的水量即为所求 10 法三:[左右两个指针] 11 找到最高的位置,将数组分为两部分: 12 对左侧数据,因为右边已经有了最高值,所以,类似法一,找到

有意思的数学题: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

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