https://leetcode.com/problems/rotating-the-box/description/
def rotateTheBox(self, boxGrid: List[List[str]]) -> List[List[str]]:
rowLength = len(boxGrid)
colLength = len(boxGrid[0])
# letting the stones fall
for r in range(rowLength):
i = colLength - 1
for c in reversed(range(colLength)):
# if stone found, replace with empty
if boxGrid[r][c] == "#":
boxGrid[r][c],boxGrid[r][i] = boxGrid[r][i], boxGrid[r][c]
i -= 1
# if obstacle found, move i pointer next to it
elif boxGrid[r][c] == "*":
i = c - 1
# now rotating the boxgrid
rotatedGrid = []
rev = boxGrid[::-1]
for i in range(colLength): # per col
r = []
for j in range(rowLength): # per row
r.append(rev[j][i])
rotatedGrid.append(r)
return rotatedGrid
trick
- shift the stones
- go thru each row, in each row, iterate from the back
- have 2 pointers: one for tracking the empty space
i
, one to iterate thru the rowc
- if stone found in
c
, then swap the position withi
then move i - if there is an obstacle in
c
, then thei
position is nowc + 1
because the rocks now can’t move beyond the obstacle
- then rotate the box
- basically the 1st column becomes the 1st row
- so basically go thru the columns from the bottom