Easy and Optimal Approach | Easy for Beginners
Intuition\n- The problem requires us to rotate a box, represented as a 2D grid, 90 degrees clockwise while adhering to specific constraints. \n- The box consist
# Intuition\n- The problem requires us to rotate a box, represented as a 2D grid, 90 degrees clockwise while adhering to specific constraints. \n- The box consists of three elements: "#" (representing rocks), "*" (representing obstacles), and "." (representing empty spaces).\n\n- The task is to simulate gravity (i.e., rocks falling towards the bottom) and when we rotate the grid 90 degrees clockwise.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. **Initialization** :\n\n    - You first initialize the dimensions m and n, representing the number of rows and columns in the box, respectively.\n    - You then create a new 2D list new_box, which will store the result after gravity is applied. \n    - Initially, all positions in new_box are set to "." (empty spaces).\n2. **Simulating Gravity (First loop through each row)** :\n\n    - For each row in the grid, you simulate the effect of gravity on the rocks.\n    - You start from the rightmost column and work your way to the left for each row. \n    - You maintain a pointer left that keeps track of the rightmost available position in the new_box where a rock can fall.\n    - For each cell:\n        - If it\u2019s a rock ("#"), you move it to the **left** pointer position, then decrement the left pointer by one step such that it goes to the next available position in the new_box.\n        - If it\u2019s an obstacle ("*"), you mark the position in new_box and update the left pointer to just before the obstacle, as rocks cannot pass through obstacles.\n        - Empty spaces (".") are skipped since rocks don\u2019t fall there.\n    - This process ensures that all the rocks fall down (or in this case, to the right) until they hit an obstacle or the end of the row.\n3. **Rotating the Box (Second part)** :\n\n    - After the gravity effect is applied, the next step is to rotate the grid 90 degrees clockwise.\n    - This can be done by flipping rows into columns. Specifically:\n        - The first column of new_box becomes the first row of the rotated box.\n        - The second column becomes the second row, and so on.\n    - This is achieved using a list comprehension that constructs each row of the rotated box by reading from the columns of new_box in **reverse row order**.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(M * N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(M * N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:\n        m = len(box)\n        n = len(box[0])\n        new_box = [["."] * n for _ in range(m)]\n        for row in range(m):\n            left = n - 1\n            for col in range(n-1,-1,-1):\n                if box[row][col] == "#":\n                    new_box[row][left] = "#"\n                    left -= 1\n                elif box[row][col] == "*":\n                    new_box[row][col] = "*"\n                    left = col - 1\n        new_box = [[new_box[m - 1 - row][col] for row in range(m)] for col in range(n)]\n        return new_box\n```
['Python3']