|
| 1 | +/* |
| 2 | + * @lc app=leetcode.cn id=407 lang=golang |
| 3 | + * |
| 4 | + * [407] 接雨水 II |
| 5 | + * |
| 6 | + * https://leetcode-cn.com/problems/trapping-rain-water-ii/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Hard (31.26%) |
| 10 | + * Likes: 93 |
| 11 | + * Dislikes: 0 |
| 12 | + * Total Accepted: 909 |
| 13 | + * Total Submissions: 2.9K |
| 14 | + * Testcase Example: '[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]' |
| 15 | + * |
| 16 | + * 给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。 |
| 17 | + * |
| 18 | + * |
| 19 | + * |
| 20 | + * 说明: |
| 21 | + * |
| 22 | + * m 和 n 都是小于110的整数。每一个单位的高度都大于 0 且小于 20000。 |
| 23 | + * |
| 24 | + * |
| 25 | + * |
| 26 | + * 示例: |
| 27 | + * |
| 28 | + * 给出如下 3x6 的高度图: |
| 29 | + * [ |
| 30 | + * [1,4,3,1,3,2], |
| 31 | + * [3,2,1,3,2,4], |
| 32 | + * [2,3,3,2,3,1] |
| 33 | + * ] |
| 34 | + * |
| 35 | + * 返回 4。 |
| 36 | + * |
| 37 | + * |
| 38 | + * |
| 39 | + * |
| 40 | + * 如上图所示,这是下雨前的高度图[[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] 的状态。 |
| 41 | + * |
| 42 | + * |
| 43 | + * |
| 44 | + * |
| 45 | + * |
| 46 | + * 下雨后,雨水将会被存储在这些方块中。总的接雨水量是4。 |
| 47 | + * |
| 48 | + */ |
| 49 | + // × testcase: '[[12,13,1,12],[13,4,13,12],[13,8,10,12],[12,13,12,12],[13,13,13,13]]' |
| 50 | +// err------------------------------- |
| 51 | + func trapRainWater(heightMap [][]int) int { |
| 52 | + rows := len(heightMap) |
| 53 | + if rows == 0 { |
| 54 | + return 0 |
| 55 | + } |
| 56 | + cols := len(heightMap[0]) |
| 57 | + if cols == 0 { |
| 58 | + return 0 |
| 59 | + } |
| 60 | + |
| 61 | + left := make([][]int, rows) |
| 62 | + for i:=0; i<rows; i++{ |
| 63 | + left[i] = make([]int, cols) |
| 64 | + } |
| 65 | + for i:=0; i<rows; i++ { |
| 66 | + left[i][0] = heightMap[i][0] |
| 67 | + for j:=1; j<cols; j++{ |
| 68 | + if heightMap[i][j] > left[i][j-1]{ |
| 69 | + left[i][j] = heightMap[i][j] |
| 70 | + } else { |
| 71 | + left[i][j] = left[i][j-1] |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + right := make([][]int, rows) |
| 77 | + for i:=0; i<rows; i++{ |
| 78 | + right[i] = make([]int, cols) |
| 79 | + } |
| 80 | + for i:=0; i<rows; i++ { |
| 81 | + right[i][cols-1] = heightMap[i][cols-1] |
| 82 | + for j:=cols-2; j>=0; j--{ |
| 83 | + if heightMap[i][j] > right[i][j+1]{ |
| 84 | + right[i][j] = heightMap[i][j] |
| 85 | + } else { |
| 86 | + right[i][j] = right[i][j+1] |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + up := make([][]int, rows) |
| 92 | + for i:=0; i<rows; i++{ |
| 93 | + up[i] = make([]int, cols) |
| 94 | + } |
| 95 | + for c:=0; c<cols;c++ { |
| 96 | + up[0][c] = heightMap[0][c] |
| 97 | + for r:=1; r<rows;r++{ |
| 98 | + if heightMap[r][c] > up[r-1][c] { |
| 99 | + up[r][c] = heightMap[r][c] |
| 100 | + }else{ |
| 101 | + up[r][c] = up[r-1][c] |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + down := make([][]int, rows) |
| 107 | + for i:=0; i<rows; i++{ |
| 108 | + down[i] = make([]int, cols) |
| 109 | + } |
| 110 | + for c:=0; c<cols;c++ { |
| 111 | + down[rows-1][c] = heightMap[rows-1][c] |
| 112 | + for r:=rows-2; r>=0;r--{ |
| 113 | + if heightMap[r][c] > down[r+1][c] { |
| 114 | + down[r][c] = heightMap[r][c] |
| 115 | + }else{ |
| 116 | + down[r][c] = down[r+1][c] |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + sum := 0 |
| 121 | + for r:=1; r<rows-1;r++{ |
| 122 | + for c:=1;c<cols-1;c++{ |
| 123 | + if heightMap[r][c] < left[r][c-1] && |
| 124 | + heightMap[r][c] < right[r][c+1] && |
| 125 | + heightMap[r][c] < up[r-1][c] && |
| 126 | + heightMap[r][c] < down[r+1][c]{ |
| 127 | + sum += min4(left[r][c-1],right[r][c+1], up[r-1][c],down[r+1][c]) - heightMap[r][c] |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return sum |
| 132 | +} |
| 133 | + |
| 134 | +func min4(a,b,c,d int) int{ |
| 135 | + if a<=b&& a<= c && a<= d{ |
| 136 | + return a |
| 137 | + }else if b<=a&& b<= c && b<= d{ |
| 138 | + return b |
| 139 | + }else if c<=a&& c<= b && c<= d{ |
| 140 | + return c |
| 141 | + } |
| 142 | + return d |
| 143 | +} |
| 144 | + |
0 commit comments