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:
Time complexity O (n), spatial complexity O (n)
For each pillar, find the tallest pillar on the left and right side, the area that the pillar can hold is min (leftmostheight,rightmostheight)-height. So
1. Scan from left to right, and for each pillar, the left maximum value is obtained;
2. Scan from right to left, for each column, the maximum right value;
3. Scan again and add up the area of each pillar.
Code
1 classSolution {2 Public: 3 intTrapintA[],intN)4 {5vector<int>Left ;6vector<int>Right ;7 left.resize (n);8 right.resize (n);9 Ten //PrintArray (A, n); One Aleft[0] ==0; -right[n-1] ==0; - the //get the left ' s Max - for(inti =1; i< n;i++) - { -Left[i] = max (left[i-1], a[i-1]); + } - //Printvector (left); + A //get the right ' s Max at for(inti = n2; i>=0; i--) - { -Right[i] = max (right[i+1], a[i+1]); - } - //Printvector (right); - in //Clac the Trap water - intsum =0; to intHeight =0; + for(inti =0; i< n;i++) - { theHeight =min (left[i], right[i]); * if(Height >A[i]) $Sum + = height-A[i];Panax Notoginseng } - returnsum; the } +};
Method Two:
Time complexity O (n), Spatial complexity O (1)
1. Scan once and find the highest pillar, which divides the array into two halves;
2. Handle the left half;
3. Handle the right half.
1 classSolution {2 Public:3 intTrapintA[],intN) {4 intMax =0; 5 for(inti =0; I < n; i++)6 if(A[i] > A[max]) max =i;7 intWater =0;8 intLeft_max_height =0;9 //Calc The left_max_height, at the same time update waterTen for(inti =0; i < Max; i++) One if(A[i] >left_max_height) ALeft_max_height =A[i]; - Else -Water + = left_max_height-A[i]; the intRight_max_height =0; - //Calc The right_max_height, at the same time update water - for(inti = n-1; i > Max; i--) - if(A[i] >right_max_height) +Right_max_height =A[i]; - Else +Water + = right_max_height-A[i]; A returnwater; at } -};
Method 3:
// LeetCode, Trapping Rain Water
// 用一个栈辅助,小于栈顶的元素压入,大于等于栈顶就把栈里所有小于或
// 等于当前值的元素全部出栈处理掉,计算面积,最后把当前元素入栈
// 时间复杂度 O(n),空间复杂度 O(n)can be used std::p air insteadstruct node structure
1 structNode2 {3 intVal;4 intindex;5 Node () {}6Node (intVintidx): Val (v), index (IDX) {}7 };8 9 classSolution {Ten Public: One intTrapintA[],intN) { AStack<node>s; - intsum =0; - for(inti =0; I < n; i++) the { - intHeight =0; - //dispose of elements in the stack that are shorter or higher than the current element - while(!s.empty ()) + { -Node node =s.top (); + //If you encounter a higher than the current element, calculate the area first, such as {4,3,2}, and then jump out of the while loop, pressing the current element stack A if(A[i] < Node.val)//a[] = {4, 2,3}, Calc sum at { - intwidth = i-node.index-1; -Sum + = a[i] * Width-height *width; - Break; - } - Else in { - //node.val, height, a[i] The hollow of the three ; to intwidth = i-node.index-1; +Sum + = Node.val * Width-height *width; -Height = node.val;//Update Height the //pop up the top of the stack because the element is finished and no longer needed * S.pop (); $ }Panax Notoginseng } - //all elements must be in the stack the S.push (Node (a[i], i)); + } A the returnsum; + } -};