1861. Rotating the Box
Intuition\n Describe your first thoughts on how to solve this problem. \nThe solution first performs a clockwise rotation of the \'box\' by assigning the values
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe solution first performs a clockwise rotation of the \'box\' by assigning the values from the original matrix box to the new matrix \'res\' in the rotated positions.\n\nNext, the solution iterates through each column of the \'res\' matrix from bottom to top. By keeping track of the start index, the solution simulates gravity by moving stones downwards until they encounter an obstacle or another stone. This is done by swapping the positions of stones and empty spaces in each column.\n\nThe resulting \'res\' matrix represents the \'box\' after the rotation and gravity simulation.\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a new matrix res with dimensions n x m to store the rotated box.\n\n2. Iterate through each cell of the original box matrix. For each cell at index (i, j), assign its value to the corresponding cell in the res matrix at index (i, m-1-j). This step performs the clockwise rotation of the box.\n\n3. Iterate through each column of the res matrix and simulate the effect of gravity on the stones. Start from the bottom of each column and move upwards. Keep track of the index start, which represents the position where the next stone should fall.\n\n4. If the current cell contains an obstacle \'*\', update the start index to j-1 to prevent stones from falling further.\n\n5. If the current cell contains a stone \'#\', update the current cell to \'.\', indicating that the stone has fallen, and assign the value \'#\' to the cell at index (start--, i). Decrementing start ensures that the next stone will fall on top of the previous one.\n\n6. Return the resulting res matrix, which represents the box after the rotation and gravity simulation.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(m * n), where m is the number of rows in the box matrix and n is the number of columns.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    The space complexity is O(m * n) as well, as we use an auxiliary space for keeping the result.\n\n---\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n        int m = box.size();\n        int n = box[0].size();\n\n        vector<vector<char>> res(n, vector<char>(m));\n        // Trasposing the matrix\n        for (int i = 0; i < n; i++) {\n            for (int j = m - 1; j >= 0; j--) {\n                res[i][m - 1 - j] = box[j][i];\n            }\n        }\n        // Applying the gravity\n        for (int i = 0; i < m; i++) {\n            int start = n - 1;\n            for (int j = start; j >= 0; j--) {\n                if (res[j][i] == \'*\') {\n                    start = j - 1;\n                } else if (res[j][i] == \'#\') {\n                    res[j][i] = \'.\';\n                    res[start][i] = \'#\';\n                    start--;\n                }\n            }\n        }\n        return res;\n    }\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\n        res = [[\'\'] * m for _ in range(n)]\n        # Transposing\n        for i in range(n):\n            for j in range(m - 1, -1, -1):\n                res[i][m - 1 - j] = box[j][i]\n        # Applying Gravity\n        for i in range(m):\n            start = n - 1\n            for j in range(start, -1, -1):\n                if res[j][i] == \'*\':\n                    start = j - 1\n                elif res[j][i] == \'#\':\n                    res[j][i] = \'.\'\n                    res[start][i] = \'#\'\n                    start -= 1\n\n        return res\n\n```\n
['Array', 'Two Pointers', 'Matrix', 'C++', 'Python3']