[Leetcode] Trapping Rain Water

Source: Internet
Author: User

Given n non-negative integers representing an elevation map where the width of each bar are 1, compute how much WA ter It is the 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) is being Trapped. thanks marcos for contributing This image!
Test Instructions: The amount of water that can be accommodated by a slot consisting of a bar of the above array,
Idea 1: First find the largest bar and then calculate the amount of water from left to right, and then select a bar that is not 0 and then, if the bar is not larger than the current one, then go into the stack, Until you find a value greater than the current, then calculate the amount of water in the bar in turn, and then repeat the above steps from right to left.
Code 1:

    public int Trap1 (list<integer> height) {//Both sides to the longest bar sweep face than the current small on the stack, until encountering a large on the pop calculate the amount of water int sum = 0;        int highindex = 0;        int max = Integer.min_value;                for (int i = 0; i < height.size (); + + i) {if (Height.get (i) > max) {max = Height.get (i);            Highindex = i;        }} int left;        stack<integer> s = new stack<integer> ();             for (int i = 0, i < Highindex;) {while (I < Highindex && Height.get (i) = = 0) {i + +;            } left = Height.get (i);                    while (i< Highindex) {When (I < Highindex && Height.get (i) <= left) {                    S.push (Height.get (i));                i++;                    } while (!s.isempty ()) {int temp = S.peek ();                    S.pop ();                Sum + = (left-temp); } left = Height.get (i);                i + +;        }} int right = 0;                for (int i = Height.size ()-1, i > highindex;) {while (i > Highindex && height.get (i) = = 0) {            I--;            } right = Height.get (i);                    while (i > Highindex) {when (i > Highindex && height.get (i) <= right) {                    S.push (Height.get (i));                I--;                    } while (!s.isempty ()) {sum + = Right-s.peek ();                S.pop ();                } right = Height.get (i);            I--;    }} return sum; }
Idea 2: Similar to the above calculation idea, this time do not have to calculate the largest bar, but each time the left and right sides selected bar size, the small one first calculation, in turn, loop. Space O (1), Time O (N)
Code:
    public int Trap (list<integer> height) {int sum = 0;        int left = 0, right = height.size ()-1;        while (left < right && Height.get (left) = = 0) {left + +;        } while (right > left && height.get (right) = = 0) {right--;        } int leftheight,rightheight;            while (left < right) {leftheight = Height.get (left);            Rightheight = Height.get (right); if (leftheight <= rightheight) {//when the left bar height is less than the right height, the left side is scanned until the left side encounters a height higher than leftheight and then the back <                    && Height.get (left) <= leftheight) {sum + = Leftheight-height.get (left);                Left + +;                    }}else {while (left < right && Height.get (right) <= rightheight) {                    Sum + = Rightheight-height.get (right);                right--;   }}} return sum; } 



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