Leetcode 407. Trapping Rain Water II

來源:互聯網
上載者:User

標籤:present   row   not   his   img   while   分享   imp   elevation   

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

Note:
Both m and n are less than 110. The height of each unit cell is 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,1]]Return 4.

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 are trapped between the blocks. The total volume of water trapped is 4.

標籤 Breadth-first Search Heap類似題目 (H) Trapping Rain Water 

 

 

思路:與2D的管住兩邊的最低高度,3D只要管住包圍的環中的最低高度就可以,保證最外圍的單元一定在優先隊列中,然後每次先遍曆原先隊列中高度最小的單元A,如果A的四周有尚未被遍曆並且高度小於A的單元B,那麼A的高度-B的高度就是新增加的蓄水體積,然後設定B的高度=max(A的高度,B的高度),將B加入優先隊列。

另外一個需要注意的是解法中對類的比較的定義,Cell執行Comparable借口,重定義compareTo(Cell o)函數:返回負數意味著當前元素“小於”o,返回正數意味著當前元素“大於”o,返回0意味著當前元素“等於”o。

compareTo函數中定義成:

return this.height - o.height;

這就意味著this.height > o.height時,Comparable返回的是當前元素“大於”o;this.height < o.height時,Comparable返回的是當前元素“小於”o。又因為JAVA的PriorityQueue是小頂堆,因此隊列的Cell是按height升序排列的。

那麼,如果想要隊列中的Cell按height降序排列,又可以怎麼寫? 

compareTo函數中可以定義成這樣:

return o.height - this.height;

這就意味著如果this.height > o.height時,Comparable返回的是當前元素“小於”o,又因為JAVA的PriorityQueue是小頂堆,因此隊列的當前元素排在o之前,也就是隊列是按降序排列的。

 

代碼:

 1 public class Solution { 2      class Cell implements Comparable<Cell> { 3         int row, col, height; 4         public Cell(int row, int col, int height) { 5             this.row = row; 6             this.col = col; 7             this.height = height; 8         } 9         //返回負數意味著當前元素“小於”o,返回正數意味著當前元素“大於”o,返回0意味著當前元素“等於”o10         //JAVA中PriorityQueue預設的是小頂堆11         public int compareTo(Cell o) {12             return this.height - o.height;13         }14     }15     16     public int trapRainWater(int[][] heightMap) {17         int n = heightMap.length, m = heightMap[0].length, res = 0;18         int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};19         boolean[][] visited = new boolean[n][m];20         PriorityQueue<Cell> queue = new PriorityQueue<Cell>();21         for (int i = 0; i < n; ++i) {22             visited[i][0] = visited[i][m - 1] = true;23             queue.offer(new Cell(i, 0, heightMap[i][0]));24             queue.offer(new Cell(i, m - 1, heightMap[i][m - 1]));25         }26         for (int i = 0; i < m; ++i) {27             visited[0][i] = visited[n - 1][i] = true;28             queue.offer(new Cell(0, i, heightMap[0][i]));29             queue.offer(new Cell(n - 1, i, heightMap[n - 1][i]));30         }31         while (!queue.isEmpty()) {32             Cell cell = queue.poll();33             for (int i = 0; i < 4; ++i) {34                 for (int j = 0; j < 2; ++j) {35                     int row = cell.row + dirs[i][j];36                     int col = cell.col + dirs[i][j];37                     if (row > 0 && row < n && col > 0 && col < m && !visited[row][col]) {38                         res += Math.max(0, cell.height - heightMap[row][col]);39                         queue.offer(new Cell(row, col, Math.max(cell.height, heightMap[row][col])));40                     }41                 }42             }43         }44         return res;45     }46 }

 

Leetcode 407. Trapping Rain Water II

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.