Skip to content

Commit b903be5

Browse files
committed
feat: add solutions to lc problem: No.0883
No.0883.Projection Area of 3D Shapes
1 parent 76a107a commit b903be5

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,53 @@
8686

8787
```
8888

89+
### **TypeScript**
90+
91+
```ts
92+
function projectionArea(grid: number[][]): number {
93+
const n = grid.length;
94+
let res = grid.reduce(
95+
(r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0),
96+
0,
97+
);
98+
for (let i = 0; i < n; i++) {
99+
let xMax = 0;
100+
let yMax = 0;
101+
for (let j = 0; j < n; j++) {
102+
xMax = Math.max(xMax, grid[i][j]);
103+
yMax = Math.max(yMax, grid[j][i]);
104+
}
105+
res += xMax + yMax;
106+
}
107+
return res;
108+
}
109+
```
110+
111+
### **Rust**
112+
113+
```rust
114+
impl Solution {
115+
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
116+
let n = grid.len();
117+
let mut res = 0;
118+
let mut x_max = vec![0; n];
119+
let mut y_max = vec![0; n];
120+
for i in 0..n {
121+
for j in 0..n {
122+
let val = grid[i][j];
123+
if val == 0 {
124+
continue;
125+
}
126+
res += 1;
127+
x_max[i] = x_max[i].max(val);
128+
y_max[j] = y_max[j].max(val);
129+
}
130+
}
131+
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
132+
}
133+
}
134+
```
135+
89136
### **...**
90137

91138
```

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,53 @@
6262

6363
```
6464

65+
### **TypeScript**
66+
67+
```ts
68+
function projectionArea(grid: number[][]): number {
69+
const n = grid.length;
70+
let res = grid.reduce(
71+
(r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0),
72+
0,
73+
);
74+
for (let i = 0; i < n; i++) {
75+
let xMax = 0;
76+
let yMax = 0;
77+
for (let j = 0; j < n; j++) {
78+
xMax = Math.max(xMax, grid[i][j]);
79+
yMax = Math.max(yMax, grid[j][i]);
80+
}
81+
res += xMax + yMax;
82+
}
83+
return res;
84+
}
85+
```
86+
87+
### **Rust**
88+
89+
```rust
90+
impl Solution {
91+
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
92+
let n = grid.len();
93+
let mut res = 0;
94+
let mut x_max = vec![0; n];
95+
let mut y_max = vec![0; n];
96+
for i in 0..n {
97+
for j in 0..n {
98+
let val = grid[i][j];
99+
if val == 0 {
100+
continue;
101+
}
102+
res += 1;
103+
x_max[i] = x_max[i].max(val);
104+
y_max[j] = y_max[j].max(val);
105+
}
106+
}
107+
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
108+
}
109+
}
110+
```
111+
65112
### **...**
66113

67114
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
3+
let n = grid.len();
4+
let mut res = 0;
5+
let mut x_max = vec![0; n];
6+
let mut y_max = vec![0; n];
7+
for i in 0..n {
8+
for j in 0..n {
9+
let val = grid[i][j];
10+
if val == 0 {
11+
continue;
12+
}
13+
res += 1;
14+
x_max[i] = x_max[i].max(val);
15+
y_max[j] = y_max[j].max(val);
16+
}
17+
}
18+
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function projectionArea(grid: number[][]): number {
2+
const n = grid.length;
3+
let res = grid.reduce(
4+
(r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0),
5+
0,
6+
);
7+
for (let i = 0; i < n; i++) {
8+
let xMax = 0;
9+
let yMax = 0;
10+
for (let j = 0; j < n; j++) {
11+
xMax = Math.max(xMax, grid[i][j]);
12+
yMax = Math.max(yMax, grid[j][i]);
13+
}
14+
res += xMax + yMax;
15+
}
16+
return res;
17+
}

0 commit comments

Comments
 (0)