Problem statement

https://binarysearch.com/problems/Rotate-a-Box-Under-Gravity/

Solution

Equal to Leetcode 1861. Rotating the Box.

Complexity

It is O(mn) for time and space.

Code

class Solution:
    def solve(self, box):
        m, n = len(box), len(box[0])
        for row in box:
            pos = n - 1
            for j in range(n - 1, -1, -1):
                if row[j] == "*":
                    pos = j - 1
                elif row[j] == "#":
                    row[pos], row[j] = row[j], row[pos]
                    pos -= 1

        return list(zip(*box[::-1]))