Leetcode:trapping Rain Water II

Source: Internet
Author: User

The height of each unit cell was greater than 0 and is less than 20,000. Example:given the following 3x6 height map:[  [1,4,3,1,3,2],  [3,2,1,3,2,4],  [ 2,3,3,2,3,14.


The above image represents the elevation map [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] before the rain.


After the rain, water is trapped between the blocks. The total volume of water trapped is 4.

Refer to HTTPS://DISCUSS.LEETCODE.COM/TOPIC/60418/JAVA-SOLUTION-USING-PRIORITYQUEUE/2

Here's a concrete example: http://www.cnblogs.com/grandyang/p/5928987.html

Analysis, according to the principle of the cask, first find the periphery of the shortest bar, inside if there is a bar than it is shorter, must be able to save water (because four weeks all bar is higher than it)

Note that it is possible to save more water, as it is likely that the cell will change in height. So to put the high bar in the middle of the BFS into the queue, as the water level rises to the height of these bars to see if there is a groove to save more

1  Public classSolution {2      Public classCell {3         intRow;4         intCol;5         intheight;6          PublicCell (intXintYintval) {7              This. Row =x;8              This. Col =y;9              This. Height =Val;Ten         } One     } A      -      Public intTraprainwater (int[] heightmap) { -         if(heightmap==NULL|| heightmap.length<=2 | | heightmap[0].length<=2)return0; the         intm =heightmap.length; -         intn = heightmap[0].length; -         intres = 0; -priorityqueue<cell> queue =NewPriorityqueue<cell> (1,NewComparator<cell>() { +              Public intCompare (cell C1, cell C2) { -                 returnC1.height-C2.height; +             } A         }); atHashset<integer> visited =NewHashset<integer>(); -          for(inti=0; i<m; i++) { -Queue.offer (NewCell (i, 0, heightmap[i][0])); -Queue.offer (NewCell (i, n-1, heightmap[i][n-1])); -Visited.add (i*n+0); -Visited.add (i*n+n-1); in         } -          for(intj=0; j<n; J + +) { toQueue.offer (NewCell (0, J, heightmap[0][j])); +Queue.offer (NewCell (M-1, J, Heightmap[m-1][j])); -Visited.add (0*n+j); theVisited.add ((m-1) *n+j); *         } $         int[] Directions =New int[][]{{-1, 0}, {1, 0}, {0, 1}, {0,-1}};Panax Notoginseng          while(!Queue.isempty ()) { -Cell cur =Queue.poll (); the              for(int[] dir:directions) { +                 introw = Cur.row + dir[0]; A                 intCol = Cur.col + dir[1]; the                 if(row>=0 && row<m && col>=0 && col<n &&!visited.contains (row*n+col)) { +Visited.add (row*n+col); -Res + = Math.max (0, Cur.height-Heightmap[row][col]); $Queue.offer (NewCell (Row, col, Math.max (cur.height, Heightmap[row][col ))); $                 } -             } -         } the         returnRes; -     }Wuyi}

Leetcode:trapping Rain Water II

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.