|
48 | 48 |
|
49 | 49 | ## Solutions
|
50 | 50 |
|
51 |
| -### Solution 1 |
| 51 | +### Solution 1: Mathematics |
| 52 | + |
| 53 | +We can calculate the area of the three projections separately. |
| 54 | + |
| 55 | +- Projection area on the xy plane: Each non-zero value will be projected onto the xy plane, so the projection area on the xy plane is the count of non-zero values. |
| 56 | +- Projection area on the yz plane: The maximum value in each row. |
| 57 | +- Projection area on the zx plane: The maximum value in each column. |
| 58 | + |
| 59 | +Finally, add up the three areas. |
| 60 | + |
| 61 | +The time complexity is $O(n^2)$, where $n$ is the side length of the grid `grid`. The space complexity is $O(1)$. |
52 | 62 |
|
53 | 63 | <!-- tabs:start -->
|
54 | 64 |
|
@@ -124,40 +134,42 @@ func projectionArea(grid [][]int) int {
|
124 | 134 |
|
125 | 135 | ```ts
|
126 | 136 | function projectionArea(grid: number[][]): number {
|
127 |
| - const n = grid.length; |
128 |
| - let res = grid.reduce((r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0), 0); |
129 |
| - for (let i = 0; i < n; i++) { |
130 |
| - let xMax = 0; |
131 |
| - let yMax = 0; |
132 |
| - for (let j = 0; j < n; j++) { |
133 |
| - xMax = Math.max(xMax, grid[i][j]); |
134 |
| - yMax = Math.max(yMax, grid[j][i]); |
135 |
| - } |
136 |
| - res += xMax + yMax; |
137 |
| - } |
138 |
| - return res; |
| 137 | + const xy: number = grid.flat().filter(v => v > 0).length; |
| 138 | + const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0); |
| 139 | + const zx: number = grid[0] |
| 140 | + .map((_, i) => Math.max(...grid.map(row => row[i]))) |
| 141 | + .reduce((acc, val) => acc + val, 0); |
| 142 | + return xy + yz + zx; |
139 | 143 | }
|
140 | 144 | ```
|
141 | 145 |
|
142 | 146 | ```rust
|
143 | 147 | impl Solution {
|
144 | 148 | pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
|
145 |
| - let n = grid.len(); |
146 |
| - let mut res = 0; |
147 |
| - let mut x_max = vec![0; n]; |
148 |
| - let mut y_max = vec![0; n]; |
149 |
| - for i in 0..n { |
150 |
| - for j in 0..n { |
151 |
| - let val = grid[i][j]; |
152 |
| - if val == 0 { |
153 |
| - continue; |
154 |
| - } |
155 |
| - res += 1; |
156 |
| - x_max[i] = x_max[i].max(val); |
157 |
| - y_max[j] = y_max[j].max(val); |
158 |
| - } |
159 |
| - } |
160 |
| - res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>() |
| 149 | + let xy: i32 = grid |
| 150 | + .iter() |
| 151 | + .map( |
| 152 | + |row| |
| 153 | + row |
| 154 | + .iter() |
| 155 | + .filter(|&&v| v > 0) |
| 156 | + .count() as i32 |
| 157 | + ) |
| 158 | + .sum(); |
| 159 | + let yz: i32 = grid |
| 160 | + .iter() |
| 161 | + .map(|row| *row.iter().max().unwrap_or(&0)) |
| 162 | + .sum(); |
| 163 | + let zx: i32 = (0..grid[0].len()) |
| 164 | + .map(|i| |
| 165 | + grid |
| 166 | + .iter() |
| 167 | + .map(|row| row[i]) |
| 168 | + .max() |
| 169 | + .unwrap_or(0) |
| 170 | + ) |
| 171 | + .sum(); |
| 172 | + xy + yz + zx |
161 | 173 | }
|
162 | 174 | }
|
163 | 175 | ```
|
|
0 commit comments