Skip to content

Commit 7266d36

Browse files
committed
feat: add solutions to lc problem: No.0883
No.0883.Projection Area of 3D Shapes
1 parent f1966da commit 7266d36

File tree

6 files changed

+223
-24
lines changed

6 files changed

+223
-24
lines changed

solution/0800-0899/0883.Projection Area of 3D Shapes/README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,53 @@
6868

6969
<!-- 这里可写通用的实现逻辑 -->
7070

71+
根据题意:
72+
73+
- xy 表示 grid 中大于 0 的元素个数
74+
- yz 表示 grid 每一行的最大值之和
75+
- zx 表示 grid 每一列的最大值之和
76+
77+
遍历 grid,更新 xy, yz, zx。遍历结束返回 `xy + yz + zx`
78+
7179
<!-- tabs:start -->
7280

7381
### **Python3**
7482

7583
<!-- 这里可写当前语言的特殊实现逻辑 -->
7684

7785
```python
78-
86+
class Solution:
87+
def projectionArea(self, grid: List[List[int]]) -> int:
88+
xy = sum(v > 0 for row in grid for v in row)
89+
yz = sum(max(row) for row in grid)
90+
zx = sum(max(col) for col in zip(*grid))
91+
return xy + yz + zx
7992
```
8093

8194
### **Java**
8295

8396
<!-- 这里可写当前语言的特殊实现逻辑 -->
8497

8598
```java
86-
99+
class Solution {
100+
public int projectionArea(int[][] grid) {
101+
int xy = 0, yz = 0, zx = 0;
102+
for (int i = 0, n = grid.length; i < n; ++i) {
103+
int maxYz = 0;
104+
int maxZx = 0;
105+
for (int j = 0; j < n; ++j) {
106+
if (grid[i][j] > 0) {
107+
++xy;
108+
}
109+
maxYz = Math.max(maxYz, grid[i][j]);
110+
maxZx = Math.max(maxZx, grid[j][i]);
111+
}
112+
yz += maxYz;
113+
zx += maxZx;
114+
}
115+
return xy + yz + zx;
116+
}
117+
}
87118
```
88119

89120
### **TypeScript**
@@ -133,6 +164,58 @@ impl Solution {
133164
}
134165
```
135166

167+
### **C++**
168+
169+
```cpp
170+
class Solution {
171+
public:
172+
int projectionArea(vector<vector<int>>& grid) {
173+
int xy = 0, yz = 0, zx = 0;
174+
for (int i = 0, n = grid.size(); i < n; ++i)
175+
{
176+
int maxYz = 0, maxZx = 0;
177+
for (int j = 0; j < n; ++j)
178+
{
179+
xy += grid[i][j] > 0;
180+
maxYz = max(maxYz, grid[i][j]);
181+
maxZx = max(maxZx, grid[j][i]);
182+
}
183+
yz += maxYz;
184+
zx += maxZx;
185+
}
186+
return xy + yz + zx;
187+
}
188+
};
189+
```
190+
191+
### **Go**
192+
193+
```go
194+
func projectionArea(grid [][]int) int {
195+
xy, yz, zx := 0, 0, 0
196+
for i, row := range grid {
197+
maxYz, maxZx := 0, 0
198+
for j, v := range row {
199+
if v > 0 {
200+
xy++
201+
}
202+
maxYz = max(maxYz, v)
203+
maxZx = max(maxZx, grid[j][i])
204+
}
205+
yz += maxYz
206+
zx += maxZx
207+
}
208+
return xy + yz + zx
209+
}
210+
211+
func max(a, b int) int {
212+
if a > b {
213+
return a
214+
}
215+
return b
216+
}
217+
```
218+
136219
### **...**
137220

138221
```

solution/0800-0899/0883.Projection Area of 3D Shapes/README_EN.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,36 @@
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def projectionArea(self, grid: List[List[int]]) -> int:
58+
xy = sum(v > 0 for row in grid for v in row)
59+
yz = sum(max(row) for row in grid)
60+
zx = sum(max(col) for col in zip(*grid))
61+
return xy + yz + zx
5762
```
5863

5964
### **Java**
6065

6166
```java
62-
67+
class Solution {
68+
public int projectionArea(int[][] grid) {
69+
int xy = 0, yz = 0, zx = 0;
70+
for (int i = 0, n = grid.length; i < n; ++i) {
71+
int maxYz = 0;
72+
int maxZx = 0;
73+
for (int j = 0; j < n; ++j) {
74+
if (grid[i][j] > 0) {
75+
++xy;
76+
}
77+
maxYz = Math.max(maxYz, grid[i][j]);
78+
maxZx = Math.max(maxZx, grid[j][i]);
79+
}
80+
yz += maxYz;
81+
zx += maxZx;
82+
}
83+
return xy + yz + zx;
84+
}
85+
}
6386
```
6487

6588
### **TypeScript**
@@ -109,6 +132,58 @@ impl Solution {
109132
}
110133
```
111134

135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
int projectionArea(vector<vector<int>>& grid) {
141+
int xy = 0, yz = 0, zx = 0;
142+
for (int i = 0, n = grid.size(); i < n; ++i)
143+
{
144+
int maxYz = 0, maxZx = 0;
145+
for (int j = 0; j < n; ++j)
146+
{
147+
xy += grid[i][j] > 0;
148+
maxYz = max(maxYz, grid[i][j]);
149+
maxZx = max(maxZx, grid[j][i]);
150+
}
151+
yz += maxYz;
152+
zx += maxZx;
153+
}
154+
return xy + yz + zx;
155+
}
156+
};
157+
```
158+
159+
### **Go**
160+
161+
```go
162+
func projectionArea(grid [][]int) int {
163+
xy, yz, zx := 0, 0, 0
164+
for i, row := range grid {
165+
maxYz, maxZx := 0, 0
166+
for j, v := range row {
167+
if v > 0 {
168+
xy++
169+
}
170+
maxYz = max(maxYz, v)
171+
maxZx = max(maxZx, grid[j][i])
172+
}
173+
yz += maxYz
174+
zx += maxZx
175+
}
176+
return xy + yz + zx
177+
}
178+
179+
func max(a, b int) int {
180+
if a > b {
181+
return a
182+
}
183+
return b
184+
}
185+
```
186+
112187
### **...**
113188

114189
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int projectionArea(vector<vector<int>>& grid) {
4+
int xy = 0, yz = 0, zx = 0;
5+
for (int i = 0, n = grid.size(); i < n; ++i)
6+
{
7+
int maxYz = 0, maxZx = 0;
8+
for (int j = 0; j < n; ++j)
9+
{
10+
xy += grid[i][j] > 0;
11+
maxYz = max(maxYz, grid[i][j]);
12+
maxZx = max(maxZx, grid[j][i]);
13+
}
14+
yz += maxYz;
15+
zx += maxZx;
16+
}
17+
return xy + yz + zx;
18+
}
19+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func projectionArea(grid [][]int) int {
2+
xy, yz, zx := 0, 0, 0
3+
for i, row := range grid {
4+
maxYz, maxZx := 0, 0
5+
for j, v := range row {
6+
if v > 0 {
7+
xy++
8+
}
9+
maxYz = max(maxYz, v)
10+
maxZx = max(maxZx, grid[j][i])
11+
}
12+
yz += maxYz
13+
zx += maxZx
14+
}
15+
return xy + yz + zx
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
class Solution {
22
public int projectionArea(int[][] grid) {
3-
int n = grid.length;
4-
int res = 0;
5-
for (int i = 0; i < n; ++i) {
3+
int xy = 0, yz = 0, zx = 0;
4+
for (int i = 0, n = grid.length; i < n; ++i) {
5+
int maxYz = 0;
6+
int maxZx = 0;
67
for (int j = 0; j < n; ++j) {
7-
res += grid[i][j] > 0 ? 1 : 0;
8+
if (grid[i][j] > 0) {
9+
++xy;
10+
}
11+
maxYz = Math.max(maxYz, grid[i][j]);
12+
maxZx = Math.max(maxZx, grid[j][i]);
813
}
14+
yz += maxYz;
15+
zx += maxZx;
916
}
10-
for (int i = 0; i < n; ++i) {
11-
int max = 0;
12-
for (int j = 0; j < n; ++j) {
13-
max = Math.max(max, grid[i][j]);
14-
}
15-
res += max;
16-
}
17-
for (int j = 0; j < n; ++j) {
18-
int max = 0;
19-
for (int i = 0; i < n; ++i) {
20-
max = Math.max(max, grid[i][j]);
21-
}
22-
res += max;
23-
}
24-
return res;
17+
return xy + yz + zx;
2518
}
26-
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def projectionArea(self, grid: List[List[int]]) -> int:
3+
xy = sum(v > 0 for row in grid for v in row)
4+
yz = sum(max(row) for row in grid)
5+
zx = sum(max(col) for col in zip(*grid))
6+
return xy + yz + zx

0 commit comments

Comments
 (0)