Trapping Rain Water--Leetcode

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 the Rain Water (blue section) is being trapped. Thanks Marcos for contributing this image!


Method One: Collect rainwater

On the left, the short side is the dam. Shorter than a levee can store a certain amount of water. Gradually collect these quantities of water. Higher than the current dam, will become a new dam.

The actual execution time of this algorithm on Leetcode is 10ms.


Class Solution {public:    int trap (int a[], int n) {        int. = 0, high = n-1;        int rain = 0, bar = 0;        while (Low < high) {                if (A[low] < A[high]) {                        if (bar < A[low])                                bar = A[low];                        else                                rain + = Bar-a[low];                        ++low;                }                else {                        if (bar < A[high])                                bar = A[high];                        else                                rain + = Bar-a[high];                        --high;                }        }        return rain;    }};


Method Two, assuming that it is rainwater, and then gradually dig out those dams occupy the amount of water.

The actual execution time of this algorithm on Leetcode is 13MS. Slower than the above, probably because of the multiplication.

Class Solution {public:    int trap (int a[], int n) {        int. = 0, high = n-1;        int bar = 0, rain = 0;        while (Low < high) {                const int newbar = MIN (A[low], A[high]);                if (Newbar > bar) {                        Rain + = (newbar-bar) * (high-low);                        bar = Newbar;                 }                if (A[low] < A[high]) {                        Rain-= min (bar, A[low]);                        ++low;                }                else {                        Rain-= min (bar, A[high]);                        --high;                }        }        return rain;    }};




Trapping Rain Water--Leetcode

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.