-
Notifications
You must be signed in to change notification settings - Fork 2
/
surface-area-of-3d-shapes.rs
53 lines (44 loc) · 1.41 KB
/
surface-area-of-3d-shapes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn surface_area(grid: Vec<Vec<i32>>) -> i32 {
let mut result = 0;
for i in 0..grid.len() {
for j in 0..grid[0].len() {
if grid[i][j] != 0 {
result += 2;
}
if i == 0 {
result += grid[i][j];
} else {
if grid[i - 1][j] < grid[i][j] {
result += grid[i][j] - grid[i - 1][j];
}
}
if i == grid.len() - 1 {
result += grid[i][j];
} else {
if grid[i + 1][j] < grid[i][j] {
result += grid[i][j] - grid[i + 1][j]
}
}
if j == 0 {
result += grid[i][j];
} else {
if grid[i][j - 1] < grid[i][j] {
result += grid[i][j] - grid[i][j - 1];
}
}
if j == grid[0].len() - 1 {
result += grid[i][j];
} else {
if grid[i][j + 1] < grid[i][j] {
result += grid[i][j] - grid[i][j + 1];
}
}
}
}
result
}
}