Simulating Gravity After Box Rotation
Intuition\n Describe your first thoughts on how to solve this problem. \nAfter rotating the box:\n The rows become columns in the rotated matrix.\n Stones (#) f
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAfter rotating the box:\n* The rows become columns in the rotated matrix.\n* Stones (#) fall downward due to gravity until blocked by an obstacle (*) or the bottom of the box.\n* Obstacles (*) remain fixed in place, unaffected by gravity.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n* Matrix Initialization:\n    1. Create a new matrix `Rbox` of size n x m initialized with . to represent the rotated box.\n\n* Simulate Rotation and Gravity:\n    1. Traverse the original matrix row by row (from bottom to top for rotation).\n    2. For each row, maintain a pointer `place` to track the lowest available position in the current column of the rotated matrix `Rbox`.\n    3. While iterating through the row from right to left:\n        - Place stones `#` at the position pointed by place and decrement place.\n        - Fix obstacles `*` at their correct positions and reset place to above the obstacle.\n\n* Return Rotated Matrix:\n\n\n# Complexity\n- Time complexity: $$O(m\u22C5n)$$\n    Each cell is visited exactly once during traversal.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m\u22C5n)$$\n    Space is used to store the rotated box (Rbox)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n        int m = box.size(), n = box[0].size();\n        vector<vector<char>> Rbox (n,vector<char>(m,\'.\'));\n        for (int i=0;i<m;i++){\n            int j = n-1;\n            int place = n-1;\n            while (j>=0){\n                if (box[m-i-1][j]==\'#\'){\n                    Rbox[place][i] = \'#\';\n                    place--; \n                }\n\n                if (box[m-i-1][j]==\'*\'){\n                    Rbox[j][i] = \'*\';\n                    place = j-1; \n                }\n                j--;\n            }\n        }\n        return Rbox;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:\n        m, n = len(box), len(box[0])\n        Rbox = [[\'.\' for _ in range(m)] for _ in range(n)]\n\n        for i in range(m):\n            # Start from the rightmost column in the row\n            place = n - 1  # Pointer for placing stones\n            for j in range(n - 1, -1, -1):\n                if box[i][j] == \'#\':\n                    Rbox[place][m - i - 1] = \'#\'\n                    place -= 1\n                elif box[i][j] == \'*\':\n                    Rbox[j][m - i - 1] = \'*\'\n                    place = j - 1\n\n        return Rbox\n```\n![image.png](https://assets.leetcode.com/users/images/b9d2a180-4d52-4cbb-9705-b5a32741b7a5_1733203963.1275604.png)\n
['Matrix', 'C++', 'Python3']