Skip to content

Commit 88a8fe0

Browse files
authored
Create projection-area-of-3d-shapes.py
1 parent 87d4a4f commit 88a8fe0

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Time: O(n^2)
2+
# Space: O(1)
3+
4+
# On a N * N grid, we place some 1 * 1 * 1 cubes
5+
# that are axis-aligned with the x, y, and z axes.
6+
#
7+
# Each value v = grid[i][j] represents a tower of
8+
# v cubes placed on top of grid cell (i, j).
9+
#
10+
# Now we view the projection of these cubes onto
11+
# the xy, yz, and zx planes.
12+
#
13+
# A projection is like a shadow, that maps our
14+
# 3 dimensional figure to a 2 dimensional plane.
15+
#
16+
# Here, we are viewing the "shadow" when looking at the cubes
17+
# from the top, the front, and the side.
18+
#
19+
# Return the total area of all three projections.
20+
#
21+
# Example 1:
22+
#
23+
# Input: [[2]]
24+
# Output: 5
25+
# Example 2:
26+
#
27+
# Input: [[1,2],[3,4]]
28+
# Output: 17
29+
# Explanation:
30+
# Here are the three projections ("shadows") of
31+
# the shape made with each axis-aligned plane.
32+
#
33+
# Example 3:
34+
#
35+
# Input: [[1,0],[0,2]]
36+
# Output: 8
37+
# Example 4:
38+
#
39+
# Input: [[1,1,1],[1,0,1],[1,1,1]]
40+
# Output: 14
41+
# Example 5:
42+
#
43+
# Input: [[2,2,2],[2,1,2],[2,2,2]]
44+
# Output: 21
45+
#
46+
# Note:
47+
# - 1 <= grid.length = grid[0].length <= 50
48+
# - 0 <= grid[i][j] <= 50
49+
50+
class Solution(object):
51+
def projectionArea(self, grid):
52+
"""
53+
:type grid: List[List[int]]
54+
:rtype: int
55+
"""
56+
result = 0
57+
for i in xrange(len(grid)):
58+
max_row, max_col = 0, 0
59+
for j in xrange(len(grid)):
60+
if grid[i][j]:
61+
result += 1
62+
max_row = max(max_row, grid[i][j])
63+
max_col = max(max_col, grid[j][i])
64+
result += max_row + max_col
65+
return result

0 commit comments

Comments
 (0)