Leetcode_42_trapping Rain Water

Source: Internet
Author: User

trouble friends to help the top to increase popularity, if there are errors or questions, please leave a message to correct, thank you

Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar are 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.

Thinking Analysis:
Find the tallest piece of wood, assuming its subscript is maxid.
Calculate from left and right to Maxid respectively.
On the left side of the calculation process:
Loop implementation from I=0 ~ I=MAXID
(1) If the height of the current plank a[i] is less than the maximum height of the previous traverse Max, i.e. A[i}<max, so I position can save max-a[i] water.
(2) If the height of the current plank a[i] is greater than the maximum height of the previous traverse Max, i.e., A[i}>=max, I cannot save water at the I position and update the max value.
The right-hand calculation process is similar to the left.


VS2012 Test Code #include<iostream>using namespace std; #define N 12class Solution {public:    int trap (int a[], int N) {int water=0;        int maxid=0;for (int i=1; i<n; i++) {if (A[i]>a[maxid]) Maxid = i;} int max = a[0];for (int i=1; i<maxid; i++) {if (A[i] >= max) max = A[i];elsewater + = Max-a[i];} max = a[n-1];for (int i=n-2; i>maxid; i--) {if (A[i] >= max) max = A[i];elsewater + = Max-a[i];} return water;}    ; int main () {int a,a[n];for (int i=0; i<n; i++) {cin>>a; A[i]=a;} Solution Lin;cout<<lin.trap (A,n) <<endl;return 0;}

Method one: Self-test accepted//thinking Analysis://1, find the tallest piece of wood, assuming its subscript is maxid. 2, respectively, from the left and right to the MAXID calculation. The left side of the calculation process://loop implementation from I=0 to i=maxid//(1) If the height of the current plank a[i] is less than the maximum height of the previous traverse Max, i.e. A[i}<max, so I position can save max-a[i] water. (2) If the height of the current plank a[i] is greater than the maximum height of the previous traverse Max, i.e., A[i}>=max, I cannot save water at the I position and update the max value. The right-hand calculation process is similar to the left. Class Solution {public:    int trap (int a[], int n) {int water=0;        int maxid=0;for (int i=1; i<n; i++) {if (A[i]>a[maxid]) Maxid = i;} int max = a[0];for (int i=1; i<maxid; i++) {if (A[i] >= max) max = A[i];elsewater + = Max-a[i];} max = a[n-1];for (int i=n-2; i>maxid; i--) {if (A[i] >= max) max = A[i];elsewater + = Max-a[i];} return water;}    ;

Method Two: stack//basic principle is to find one of the small container interval. Requires two stacks and a combined interval function mate. The first stack S1 is of type Stack<pair<int, int>, which is used to hold the container interval, the second stack S2 is of type Stack<int&gt, and the location where the boundary is to be saved. The idea is as follows://1, if the stack s2 is empty, press the current position onto the stack. 2. If the stack S2 is not empty://2.1, if the value of the top position of the stack is greater than the current position, press the current position onto the stack. (A[i] < A[s2.top ()])//2.2, if the previous bar is not satisfied, then the value of the current position must be greater than the value of the position at the top of the stack. (A[i] >= a[s2.top ())//(1) If the stack S2 is not empty and updates the left edge position to the top of the stack, if the value on the left edge is greater than the current position value, stop the loop, or the top element of the stack pops up and continues the comparison. (2) Press the current position onto the stack. (3) Press the position of the current element into the stack. At the same time, the position of the left and current elements is exactly a container interval, according to the plank principle, the volume is determined by the smaller value. If S1 is not empty, the top element of the stack can be merged with the current element, the top element of the pop-up stack is merged with the current interval, and the top element of the stack is checked, and if the stack is empty or the stack top element cannot be merged with the current interval, the current element is pressed onto the stack. 3, Traverse S1 and calculate volume.  Class Solution {Public:bool Isinrange (pair<int, int> &left, Pair<int, int> &right) {return    (Left.first >= Right.first) && (Left.second <= right.second);        } int Getvolume (int a[], pair<int, int> range) {int left = Range.first, right = Range.second;                int volume = min (A[left], a[right]) * (RIGHT-LEFT-1); for (int i = left + 1, i < right; ++i) {volume-= A[i];    } return volume;        } int Trap (int a[], int n) {stack<pair<int, int> > ranges;                stack<int> bars;            for (int i = 0; i < n; ++i) {if (Bars.empty ()) {Bars.push (i);                } else {if (A[i] < A[bars.top ()]) {Bars.push (i);                    } else {int left;                                        Pair<int, int> Range;                        while (!bars.empty ()) {left = Bars.top ();                        if (A[left] > A[i]) {break;                    } bars.pop ();                    } bars.push (i);                    Range.first = left;                                        Range.second = i;               while (!ranges.empty ()) {         if (Isinrange (Ranges.top (), range)) {Ranges.pop ();                        } else {break;                }} ranges.push (range);                }}} int volume = 0;            while (!ranges.empty ()) {volume + = Getvolume (A, Ranges.top ());        Ranges.pop ();    } return volume; }};


Leetcode_42_trapping Rain Water

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.