Leetcode 407. Trapping Rain Water II

Source: Internet
Author: User
Tags comparable

Given m x n a matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the VO Lume of water it is able to trap after raining.

Note:
Both m and N is less than 110. The height of each unit cell is greater than 0 and was 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.

tags breadth-first Search HeapSimilar topics(H) trapping Rain water

Train of thought: With the 2D of the two sides of the minimum height, 3D as long as the surrounding ring in the minimum height can be, to ensure that the outermost unit must be in the priority queue, and then each time first traverse the original queue of the smallest cell a, if the perimeter of a has not been traversed and the height is less than a cell B, Then the height of a-B is the newly added storage volume, then the height of B is set to =max (the height of a, the height of B), and B is added to the priority queue.

Another thing to note is the definition of the comparison of classes in the solution, the cell performs comparable excuses, and the CompareTo (cell O) function is redefined: returning a negative number means that the current element is "less than" O, and returning a positive number means that the current element is "greater than" O, and returning 0 means that the current element "equals "O.

The CompareTo function is defined as:

return this. height-o.height;

This means that when This.height > O.height, comparable returns the current element "greater than" O;this.height < O.height, comparable returns the current element "less than" O. And because Java's priorityqueue is a small top heap, the queue's cells are arranged in ascending order of height.

So, what can I do if I want cells in the queue to be sorted in descending order of height?

The CompareTo function can be defined as such:

return this. Height;

This means that if this.height > o.height, comparable returns the current element is "less than" O, and because Java's priorityqueue is a small top heap, the current element of the queue is before O, that is, the queue is sorted in descending order.

Code:

1  Public classSolution {2      classCellImplementsComparable<cell> {3         intRow, col, height;4          PublicCell (intRowintColintheight) {5              This. Row =Row;6              This. Col =Col;7              This. Height =height;8         }9         //returning a negative number means that the current element is "less than" O, and returning a positive number means that the current element is "greater than" O, and returning 0 means that the current element is "equal to" OTen         //priorityqueue Default in Java is the small top heap One          Public intcompareTo (Cell o) { A             return  This. Height-O.height; -         } -     } the      -      Public intTraprainwater (int[] heightmap) { -         intn = heightmap.length, M = heightmap[0].length, res = 0; -         int[] dirs =New int[][]{{-1, 0}, {1, 0}, {0,-1}, {0, 1}}; +         Boolean[] visited =New Boolean[n][m]; -priorityqueue<cell> queue =NewPriorityqueue<cell>(); +          for(inti = 0; I < n; ++i) { AVisited[i][0] = visited[i][m-1] =true; atQueue.offer (NewCell (i, 0, heightmap[i][0])); -Queue.offer (NewCell (i, M-1, heightmap[i][m-1])); -         } -          for(inti = 0; I < m; ++i) { -Visited[0][i] = Visited[n-1][i] =true; -Queue.offer (NewCell (0, I, heightmap[0][i])); inQueue.offer (NewCell (n-1, I, heightmap[n-1][i])); -         } to          while(!Queue.isempty ()) { +Cell cell =Queue.poll (); -              for(inti = 0; I < 4; ++i) { the                  for(intj = 0; J < 2; ++j) { *                     introw = Cell.row +Dirs[i][j]; $                     intCol = Cell.col +Dirs[i][j];Panax Notoginseng                     if(Row > 0 && row < n && col > 0 && col < m &&!)Visited[row][col]) { -Res + = Math.max (0, Cell.height-Heightmap[row][col]); theQueue.offer (NewCell (Row, col, Math.max (cell.height, Heightmap[row][col ))); +                     } A                 } the             } +         } -         returnRes; $     } $}

Leetcode 407. 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.