Leetcode Portal https://leetcode.com/problems/trapping-rain-water/
Target: Find the "area" of water that the bricks can hold, the black part is the building block, the blue is the part that can hold the water.
Assumption: The brick width is 1
Input: Height of each building block
Output: All blocks can accommodate the "area" of water
Thinking process
1. It seems not easy to find the spacing between the bricks individually. Especially for the volume between the 3-7 bricks in the figure, if the volume of the bottom (4-6) can be obtained first, when the upper (3-7) volume is solved, additional processing is needed, such as reducing the height of the bottom.
2. In this case, can we first find out the volume of the 3-7 whole, and then subtract the block area between the two bricks (4, 5, 6) to get the volume of this area?
3. Then the problem is converted, known to a building block, how to find the next building block, and calculate the volume between the two bricks
4. Try one, looking for a building block that is at least less than the current building block, as the next building block, example of the current building block 3, the next building block 7. So how is the volume calculated, simple, the height of the building block 3 times the spacing between the two bricks, minus the block 3 and the building Block 7 (4, 5, 6) area.
5. Question: Where does the next iteration begin? In the previous example, you can continue counting from block 7. However, if the corresponding building blocks can not be found, then from the building Block 3, the next block 4 start.
6. It seems to be correct. As a start, write the code. However, the first submission was dedicated to WA (/(ㄒoㄒ)/~~)
7. In the wrong case, if the current building block is a particularly high building block, the successor can not find a higher building blocks, but in fact there may be a volume! Not to mention, the remedial idea is: in the search for higher building blocks at the same time, the record is currently found the tallest building blocks. If a higher building block is not found, use the tallest building block currently found as the next building block and calculate the volume.
Summary of Ideas
The process of iteration
1. Look for the next building block that is at least less than the current one, and keep track of the tallest block height currently found.
2. If found, calculate the volume between the two bricks (the process of calculation see thinking Process 2)
3. If not found, calculates the volume between the current building block and the tallest block currently found
4. The current building block is set to the found building block (which may be 2 or 3 cases), continue iterating
Complexity of Time
O (N)
Code implementation
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 classSolution {7 Public:8 intTrap (vector<int>&height) {9 if(height.size () = =0) {Ten return 0; One } A - intWater =0; - intPreindex =0; the intPreheight =0; - - //find the first bar with a height not 0 - while(Preindex < Height.size ()-1&& Height[preindex] = =0) { +++Preindex; - } + A if(Preindex = = Height.size ()-1) { at return 0; - } - - //Traverse - while(Preindex <height.size ()) { -Preheight =Height[preindex]; in - //looking for a higher or equal bar to //record the highest bar that has been traversed at the same time + //if a higher or equal bar is not found, the record value is used to calculate - intNext = Preindex +1; the intMinHeight =0; * intMinindex =Next; $ Panax Notoginseng while(Next < Height.size () && Height[next] <preheight) { - if(Height[next] >minheight) { theMinHeight =Height[next]; +Minindex =Next; A } the++Next; + } - $ //If you find $ if(Next! =height.size ()) { -Water + = (Preheight * (Next-preindex-1));//Calculate total area - the for(inti = Preindex +1; i < Next; ++i) {//minus the center bar area -Water-=Height[i];Wuyi } the -Preindex = Next;//start the next iteration from the bar found Wu}Else { -Water + = (MinHeight * (Minindex-preindex-1));//Calculate total area About $ for(inti = Preindex +1; i < Minindex; ++i) {//minus the center bar area -Water-=Height[i]; - } - APreindex = Minindex;//starting at the second highest bar + } the } - $ returnwater; the } the }; the the intMainintargcChar Const*argv[]) { - solution solution; invector<int> height = {0,1,0,2,1,0,1,3,2,1,2,1}; thecout << solution.trap (height) <<Endl; the return 0; About}trapping Rain Water
Chit chat one or two
Lunar New Year Wish everyone monkey happy, already work of children's shoes promotion raise salary, want to intern, look for work of children shoes offer a lot ~ Bo Master this year also face looking for internships, find a job life big thing. Long time no blog, silently mourn the last semester, their blind toss, out of the immature things. Hope Monkey is a passion, struggle, harvest of a year ~
PS: There is no interested in my boss shelter ~ with a friend address a https://github.com/zrss
Simple rough Resume:https://github.com/zrss/ghostblog, recently revised
Interesting math problem: trapping Rain water