-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path892-SurfaceAreaOf3DShapes.go
More file actions
69 lines (59 loc) · 2.19 KB
/
892-SurfaceAreaOf3DShapes.go
File metadata and controls
69 lines (59 loc) · 2.19 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
// 892. Surface Area of 3D Shapes
// You are given an n x n grid where you have placed some 1 x 1 x 1 cubes.
// Each value v = grid[i][j] represents a tower of v cubes placed on top of cell (i, j).
// After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
// Return the total surface area of the resulting shapes.
// Note: The bottom face of each shape counts toward its surface area.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid2.jpg" />
// Input: grid = [[1,2],[3,4]]
// Output: 34
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid4.jpg" />
// Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
// Output: 32
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid5.jpg" />
// Input: grid = [[2,2,2],[2,1,2],[2,2,2]]
// Output: 46
// Constraints:
// n == grid.length == grid[i].length
// 1 <= n <= 50
// 0 <= grid[i][j] <= 50
import "fmt"
func surfaceArea(grid [][]int) int {
res, n := 0, len(grid)
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if grid[i][j] > 0 {
res += grid[i][j] * 4 + 2
}
if i > 0 {
res -= (min(grid[i][j], grid[i - 1][j]) * 2)
}
if j > 0 {
res -= (min(grid[i][j], grid[i][j - 1]) * 2)
}
}
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid2.jpg" />
// Input: grid = [[1,2],[3,4]]
// Output: 34
fmt.Println(surfaceArea([][]int{{1,2},{3,4}})) // 34
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid4.jpg" />
// Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
// Output: 32
fmt.Println(surfaceArea([][]int{{1,1,1},{1,0,1},{1,1,1}})) // 32
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid5.jpg" />
// Input: grid = [[2,2,2],[2,1,2],[2,2,2]]
// Output: 46
fmt.Println(surfaceArea([][]int{{2,2,2},{2,1,2},{2,2,2}})) // 46
}