-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1861_Rotating_the_Box.py
More file actions
76 lines (54 loc) · 2.01 KB
/
Copy path1861_Rotating_the_Box.py
File metadata and controls
76 lines (54 loc) · 2.01 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
70
71
72
73
74
75
76
"""
You are given an m x n matrix of characters boxGrid representing a side-view of a box. Each cell of the box is one of the following:
A stone '#'
A stationary obstacle '*'
Empty '.'
The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.
It is guaranteed that each stone in boxGrid rests on an obstacle, another stone, or the bottom of the box.
Return an n x m matrix representing the box after the rotation described above.
Example 1:
Input: boxGrid = [["#",".","#"]]
Output: [["."],
["#"],
["#"]]
Example 2:
Input: boxGrid = [["#",".","*","."],
["#","#","*","."]]
Output: [["#","."],
["#","#"],
["*","*"],
[".","."]]
Example 3:
Input: boxGrid = [["#","#","*",".","*","."],
["#","#","#","*",".","."],
["#","#","#",".","#","."]]
Output: [[".","#","#"],
[".","#","#"],
["#","#","*"],
["#","*","."],
["#",".","*"],
["#",".","."]]
Constraints:
m == boxGrid.length
n == boxGrid[i].length
1 <= m, n <= 500
boxGrid[i][j] is either '#', '*', or '.'.
"""
class Solution:
def rotateTheBox(self, boxGrid: List[List[str]]) -> List[List[str]]:
rows ,cols = len(boxGrid) , len(boxGrid[0])
res=[]
for r in range(rows):
i = cols - 1
for c in reversed(range(cols)):
if boxGrid[r][c] == "#":
boxGrid[r][c] , boxGrid[r][i] = boxGrid[r][i] , boxGrid[r][c]
i -= 1
elif boxGrid[r][c] =="*":
i = c - 1
for c in range(cols):
col = [] #row after rotation
for r in reversed(range(rows)):
col.append(boxGrid[r][c])
res.append(col)
return res