First of all, the initial but incorrect idea, the STK deposit value, is initialized to the first bit of the input array. Read from left to right into the array each bit, if height[i]>stk.top (), indicating that stk.top () can not water storage, pop off, replace with height[i]; if Height[i]<=stk.top (), then from I start traversing, Until you find a height[i that is greater than or equal to Stk.top (), or until the last one is found, maintain the amount of the variable TMP storage, but if you do not find a height[i greater than or equal to Stk.top (), you cannot actually water the water, That is, TMP is meaningless (so open the variable tmp instead of directly to RES), in which case Stk wants pop and then push into the next height of the original top value, so also open a variable top_pos, storing the position of stk.top () in the array If you can find a height[i that is greater than or equal to Stk.top (), RES+=TMP,STK will pop,push into stk.top that is currently greater than or equal to Height[i ().
In fact, do not open STACK,STK should have only one data, because see tag is stack so opened a, actually int variable can.
1 classSolution {2 Public:3 intTrap (vector<int>&height) {4 intSize =height.size ();5 if(Size = =0)return 0;6 inti =0, Top_pos =0;7 intres =0;8stack<int>Stk;9Stk.push (height[i++]);Ten for(; i < size; + +)i) { One if(Height[i] >Stk.top ()) { A Stk.pop (); - Stk.push (Height[i]); -Top_pos =i; the } - Else{ - intTMP =0; - while(I <= Size-1&& Height[i] <Stk.top ()) { +TMP + = Stk.top ()-Height[i]; -++i; + } A if(i = = size) i = size-1; at if(Height[i] <Stk.top ()) { - Stk.pop (); -i = + +Top_pos; - Stk.push (Height[i]); - } - Else{ inRes + =tmp; - Stk.pop (); to Stk.push (Height[i]); +Top_pos =i; - } the } * } $ returnRes;Panax Notoginseng } -};
This algorithm 140/315 test cases passed.
Input:[4,2,3] Output:0 expected:1 Look at other people's algorithms, without stack pure logic to solve, explain. The addition of water: from left to right, water+= before highest current maximum (excluding height[i]), that is, only the current height and the maximum height before the storage. Water: From right-to-left traversal, water-= before highest current maximum (including height[i]), if the current maximum < overall maximum, indicating the current height at the highest height of the right, some water can not be stored, The amount of water that cannot be stored is the overall maximum-the current maximum; If the current maximum value >= the overall maximum, indicating that the current height is at the highest height of the left or the highest height, and in addition, the storage capacity only and the height of the left side of the maximum value, so the previous added water storage is reasonable, There is no need to subtract anything.
1 classSolution {2 Public:3 intTrap (vector<int>&height) {4 intSz=height.size (), highest=0, water=0;5 //From left to right, only consider the trap 's left elevation6 for(intI=0; i<sz; i++){7 if(height[i]Height[i];8highest=Max (highest, height[i]);9 }Ten One intprehighest=highest; Ahighest=0; - //From consider the trap's right elevation, subtract the surplus water - for(inti=sz-1; i>=0; i--){ thehighest=Max (Height[i], highest); - if(highest<prehighest) water-=prehighest-highest; - } - returnwater; + } -};
There is no stack algorithm to look at, leave the pit.
Leetcode 42. Trapping Rain Water