[Leetcode] Trapping Rain Water

Source: Internet
Author: User

Problem Description:

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!


Basic ideas:

Through analysis, it can be found that the tank of the storage water on both sides of the trough height of the maximum value. (Here for the left-most Cao, as long as the greater than equal to the right of it.) The right side of the same)

But the simple consideration of each of the two maximum slots as a result of both sides of the sink is wrong. As an example:

A = {9, 2, 9, 2,2,1,8}

Here if only the water stored between each of the two Maxima is considered, the result is 7+0+1 = 8. In fact, the biggest result should be 7+6+6+7 = 26.

So, to the left slot with no computed left slot, find the slot with one of the following conditions as the right slot:

    1. Groove height of the Groove >= the leftmost groove height
    2. The groove is the largest of the slots in the right slot of the left slot.

(PS: I wrote this question for a long time ...)  There's always a place to go wrong. It was later found that the position of local maxima and local maxima was mistaken-.-| | )


Code:

    int trap (int a[], int n) {//c++ if (n < 3) return 0;                Vector<int> Locmaxs;        int sum = 0;        int i = 0; while (I < n) {if (i = = 0 && a[0] >= a[1]) {locmaxs.                    Push_back (0);                                    i++; } else if (i = = n-1 && a[n-1] >= a[n-2]) {locmaxs.push_back (n-                    1);                                                                                   i++; } else if (A[i] >= a[i-1] && a[i] >= a[i+1]) {Locmaxs.push_                    Back (i);                i++;        } else i++;        } for (i = 0; i < locmaxs.size ()-1;)            {int pos = i+1, max = locmaxs[i+1];  for (int j = i+1; J < Locmaxs.size (); J + +) {IF (A[locmaxs[j]] >= a[locmaxs[i]) {pos = j;                Break                    } if (A[locmaxs[j]] > A[max]) {max = locmaxs[j];                pos = j; }} int draw = ((A[locmaxs[i] > A[locmaxs[pos])?            A[locmaxs[pos]]: a[locmaxs[i]); for (int j = locmaxs[i]+1; J < Locmaxs[pos]; j + +) if (draw-a[j] >0) sum + = Draw-            A[J];        i = pos;    } 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.