Leetcode trapping Rain Water

Source: Internet
Author: User

classSolution { Public:    intTrapintA[],intN) {if(N <1)return 0; Vector<int>Peaks; Vector<int>peaks_tmp; intLast_idx =0; BOOLincreasing =true;  for(intI=1; i<n; i++) {            if(A[i] <A[last_idx]) {                if(increasing) {peaks.push_back (LAST_IDX); } Increasing=false; } Else{Increasing=true; } last_idx=i; }        if(increasing) Peaks.push_back (N-1); if(Peaks.size () <2)return 0; BOOLUpdated =true;  while(updated) {Updated=false;            Peaks_tmp.clear (); Peaks_tmp.push_back (peaks[0]); Peaks_tmp.push_back (peaks[1]);  for(intI=2; I<peaks.size (); i++) {                intTlen =peaks_tmp.size (); intAi = Peaks_tmp[tlen-2]; intBI = Peaks_tmp[tlen-1]; intCI =Peaks[i]; if(A[ai] >= A[bi] && A[ci] >=A[bi]) {Peaks_tmp[tlen-1] =CI; Updated=true; } Else{peaks_tmp.push_back (CI);        }} swap (peaks, peaks_tmp); }        intRain =0;  for(intI=1; I<peaks.size (); i++) {            intleft = peaks[i-1]; intright=Peaks[i]; inth =min (A[left], a[right]); intBlocks =0;  for(intI=left +1; i<right; i++) blocks + = A[i] > h?H:a[i]; Rain+ = h * (Right-left-1) -blocks; }        returnRain; }};

The peaks of each block are calculated first, and then the rain must fall between the peak blocks, merging the bands bounded by the peak blocks (for sequence a{4,1,3,1,5}, the first interval is a (0,2), the second interval is a (2,4), because a[4] > a[2] and a[0] > a[2] So can be combined into intervals a (0, 4)), until the merger cannot be continued, ultimately calculating the amount of water that can accumulate in these block intervals.

A little bit better, the merging process takes place at peak time, and the extra array is reduced to a

classSolution { Public:        intTrapintA[],intN) {if(N <1)return 0; Vector<int>Peaks; intLast_idx =0; BOOLincreasing =true;  for(intI=1; i<n; i++) {            if(A[i] <A[last_idx]) {                if(increasing) {peaks.push_back (LAST_IDX);                Compact (A, peaks); } Increasing=false; } Else{Increasing=true; } last_idx=i; }                if(increasing) Peaks.push_back (N-1);                Compact (A, peaks); if(Peaks.size () <2)return 0; intRain =0;  for(intI=1; I<peaks.size (); i++) {            intleft = peaks[i-1]; intright=Peaks[i]; inth =min (A[left], a[right]); intBlocks =0;  for(intI=left +1; i<right; i++) blocks + = A[i] > h?H:a[i]; Rain+ = h * (Right-left-1) -blocks; }        returnRain; }    voidCompactintA[], vector<int> &peaks) {        intLen =peaks.size (); if(Len <3)return; BOOLUpdated =true;  while(Updated && (len = peaks.size ()) >=3) {Updated=false; intAi = Peaks[len-3]; intBI = Peaks[len-2]; intCI = Peaks[len-1]; if(A[ai] >= A[bi] && A[ci] >=A[bi]) {Peaks[len-2] =CI;                Peaks.pop_back (); Updated=true; }        }    }};

Another simple way to see: http://www.cnblogs.com/zhuli19901106/p/3542216.html, as follows

classSolution { Public:    intTrapintA[],intN) {if(N <2)return 0; Vector<int> Lmax (N,0); Vector<int> Rmax (N,0); intm = a[n-1];  for(inti=n-2; I >=0; i--) {            if(A[i] > m) m =A[i]; Rmax[i]=m; } m= a[0];  for(intI=1; i<n; i++) {            if(A[i] > m) m =A[i]; Lmax[i]=m; }        intRain =0;  for(intI=0; i<n; i++) {            inth =min (lmax[i], rmax[i]); intv = h-A[i]; Rain+ = v <0?0: v; }        returnRain; }};

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.