-
Notifications
You must be signed in to change notification settings - Fork 43
/
projection-area-of-3d-shapes.py
62 lines (59 loc) · 1.66 KB
/
projection-area-of-3d-shapes.py
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
# V1 : dev
# V2
# https://blog.csdn.net/fuxuemingzhu/article/details/81748335
class Solution(object):
def projectionArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
top, front, side = 0, 0, 0
n = len(grid)
for i in range(n):
x, y = 0, 0
for j in range(n):
if grid[i][j] != 0:
top += 1
x = max(x, grid[i][j])
y = max(y, grid[j][i])
front += x
side += y
return top + front + side
# V3
# https://www.cnblogs.com/seyjs/p/9538305.html
class Solution(object):
def projectionArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
front = [0] * len(grid)
side = [0] * len(grid)
top = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 0:
continue
top += 1
front[i] = max(front[i],grid[i][j])
side[j] = max(side[j],grid[i][j])
return top + sum(front) + sum(side)
# V4
# Time: O(n^2)
# Space: O(1)
class Solution(object):
def projectionArea(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
result = 0
for i in range(len(grid)):
max_row, max_col = 0, 0
for j in range(len(grid)):
if grid[i][j]:
result += 1
max_row = max(max_row, grid[i][j])
max_col = max(max_col, grid[j][i])
result += max_row + max_col
return result