-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotating-the-box.py
More file actions
28 lines (25 loc) · 938 Bytes
/
rotating-the-box.py
File metadata and controls
28 lines (25 loc) · 938 Bytes
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
class Solution:
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
'''
Simulation:
To rotate the box, take the transpose but row should inserted from the end.
Maintain 2 Pointers top, bottom. If top encouters a stone place it at bottom.
Else if top encounters an obstacle, bring bottom pointer to top - 1.
'''
m = len(box)
n = len(box[0])
ans = [["."] * m for _ in range(n)]
for i in range(m):
for j in range(n):
ans[j][m - i - 1] = box[i][j]
for col in range(m):
top, bottom = n - 1, n - 1
while top >= 0:
if ans[top][col] == "#":
ans[top][col] = "."
ans[bottom][col] = "#"
bottom -= 1
if ans[top][col] == "*":
bottom = top - 1
top -= 1
return ans