Info

Be VERY careful not to do:

  • rotatedArray = [[-1] * m] * n

That would pass by reference, and override values in so doing.

My Solution

class Solution:
    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
        def swap(posWithTheRock, posWithTheEmptySquare, row):
            temp = box[row][posWithTheEmptySquare]
            box[row][posWithTheEmptySquare] = box[row][posWithTheRock]
            box[row][posWithTheRock] = temp
 
        def shiftRight(rowIndex, rockToShiftPos):
            row = box[rowIndex]
            for i in range(rockToShiftPos, len(row) - 1):
                if row[i + 1] == '*' \
                    or row[i + 1] == '#':
                    break
                else:
                    swap(i, i + 1, rowIndex)
        
        def rotateArray():
            # Get the number of columns
            m = len(box) # Number of rows
            n = len(box[0]) # Number of columns
 
            # rotatedArray = [[-1] * m] * n
 
            rotatedArray = []
            for i in range(n):
                rotatedArray.append([])
                for j in range(m):
                    rotatedArray[i].append(-1)
 
            k = 0
            l = m - 1
 
            for i in range(m):
                for j in range(n):
                    rotatedArray[k][l] = box[i][j]
                    k += 1
 
                k = 0
                l -= 1
 
            return rotatedArray
 
        for rowIndex, row in enumerate(box):
            rowLength = len(row)
 
            for i in range(rowLength - 1,  -1, -1):
                if row[i] == '#':
                    shiftRight(rowIndex, i)
 
        # Allocate a new 2D array of the inverse size
        rotatedArray = rotateArray()
 
        return rotatedArray


References

https://leetcode.com/problems/rotating-the-box/description/