🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏
\n\n---\n\n\n\n## Intuition\nThe problem involves two main tasks:\n1. Rotating a 2D grid: Rotating the box 90 degrees clockwise allows us to reorient the box fo
![image.png](https://assets.leetcode.com/users/images/108c7273-ab5b-4160-abc7-62f583009f93_1732897012.7079284.png)\n\n---\n\n\n\n## Intuition\nThe problem involves two main tasks:\n1. **Rotating a 2D grid**: Rotating the box 90 degrees clockwise allows us to reorient the box for simulating gravity.\n2. **Simulating gravity**: After the rotation, stones (`#`) in each column need to fall to their lowest possible positions, stopping when blocked by obstacles (`*`).\n\n---\n\n## Approach\n1. **Rotate the grid**:\n   - A 90-degree clockwise rotation of a grid is achieved by transposing the matrix and then reversing each row.\n2. **Apply gravity**:\n   - Iterate through each column of the rotated grid.\n   - Move stones (`#`) downward while avoiding obstacles (`*`).\n\n---\n\n## Complexity\n- **Time Complexity**:  \n   \\(O(m \\cdot n)\\), where \\(m\\) is the number of rows and \\(n\\) is the number of columns in the box.  \n   - \\(O(m \\cdot n)\\) for rotating the box.  \n   - \\(O(m \\cdot n)\\) for applying gravity.\n- **Space Complexity**:  \n   \\(O(m \\cdot n)\\) for storing the rotated grid.\n\n---\n\n\n```java []\npublic class Solution {\n    public char[][] rotateTheBox(char[][] box) {\n        int m = box.length;\n        int n = box[0].length;\n        \n        // Step 1: Rotate the matrix 90 degrees clockwise\n        char[][] rotated = new char[n][m];\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                rotated[j][m - 1 - i] = box[i][j];\n            }\n        }\n        \n        // Step 2: Simulate gravity for stones\n        for (int col = 0; col < n; col++) {\n            int ind = m - 1;\n            for (int row = m - 1; row >= 0; row--) {\n                if (rotated[col][row] == \'#\') {\n                    rotated[col][row] = \'.\';\n                    rotated[col][ind] = \'#\';\n                    ind--;\n                } else if (rotated[col][row] == \'*\') {\n                    ind = row - 1;\n                }\n            }\n        }\n        \n        return rotated;\n    }\n}\n```\n\n\n```dart []\nclass Solution {\n  List<List<String>> rotateTheBox(List<List<String>> box) {\n    int m = box.length;\n    int n = box[0].length;\n\n    // Step 1: Rotate the matrix 90 degrees clockwise\n    List<List<String>> rotated = List.generate(n, (_) => List.filled(m, \'.\'));\n\n    for (int i = 0; i < m; i++) {\n      for (int j = 0; j < n; j++) {\n        rotated[j][m - 1 - i] = box[i][j];\n      }\n    }\n\n    // Step 2: Simulate gravity for stones\n    for (int col = 0; col < m; col++) {\n      int ind = n - 1;\n      for (int row = n - 1; row >= 0; row--) {\n        if (rotated[row][col] == \'#\') {\n          rotated[row][col] = \'.\';\n          rotated[ind][col] = \'#\';\n          ind--;\n        } else if (rotated[row][col] == \'*\') {\n          ind = row - 1;\n        }\n      }\n    }\n\n    return rotated;\n  }\n}\n```\n\n\n```javascript []\nvar rotateTheBox = function(box) {\n    const m = box.length, n = box[0].length;\n\n    // Step 1: Rotate the matrix 90 degrees clockwise\n    const rotated = Array.from({ length: n }, () => Array(m).fill(\'.\'));\n    for (let i = 0; i < m; i++) {\n        for (let j = 0; j < n; j++) {\n            rotated[j][m - 1 - i] = box[i][j];\n        }\n    }\n\n    // Step 2: Simulate gravity\n    for (let col = 0; col < n; col++) {\n        let ind = m - 1;\n        for (let row = m - 1; row >= 0; row--) {\n            if (rotated[col][row] === \'#\') {\n                rotated[col][row] = \'.\';\n                rotated[col][ind--] = \'#\';\n            } else if (rotated[col][row] === \'*\') {\n                ind = row - 1;\n            }\n        }\n    }\n\n    return rotated;\n};\n```\n\n\n```python []\nclass Solution:\n    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:\n        m, n = len(box), len(box[0])\n        \n        # Step 1: Rotate the box 90 degrees clockwise\n        rotated = [[box[i][j] for i in range(m)] for j in range(n - 1, -1, -1)]\n        \n        # Step 2: Apply gravity\n        for col in range(m):\n            ind = n - 1\n            for row in range(n - 1, -1, -1):\n                if rotated[row][col] == \'#\':\n                    rotated[row][col] = \'.\'\n                    rotated[ind][col] = \'#\'\n                    ind -= 1\n                elif rotated[row][col] == \'*\':\n                    ind = row - 1\n        \n        return rotated\n```\n\n\n```cpp []\nclass Solution {\npublic:\n    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n        int m = box.size(), n = box[0].size();\n        \n        // Step 1: Rotate the box 90 degrees clockwise\n        vector<vector<char>> rotated(n, vector<char>(m, \'.\'));\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                rotated[j][m - 1 - i] = box[i][j];\n            }\n        }\n        \n        // Step 2: Simulate gravity\n        for (int col = 0; col < n; col++) {\n            int ind = m - 1;\n            for (int row = m - 1; row >= 0; row--) {\n                if (rotated[col][row] == \'#\') {\n                    rotated[col][row] = \'.\';\n                    rotated[col][ind--] = \'#\';\n                } else if (rotated[col][row] == \'*\') {\n                    ind = row - 1;\n                }\n            }\n        }\n        \n        return rotated;\n    }\n};\n```\n\n\n```go []\nfunc rotateTheBox(box [][]byte) [][]byte {\n    m, n := len(box), len(box[0])\n\n    // Step 1: Rotate the matrix 90 degrees clockwise\n    rotated := make([][]byte, n)\n    for i := 0; i < n; i++ {\n        rotated[i] = make([]byte, m)\n        for j := 0; j < m; j++ {\n            rotated[i][j] = box[m-1-j][i]\n        }\n    }\n\n    // Step 2: Apply gravity\n    for col := 0; col < m; col++ {\n        ind := n - 1\n        for row := n - 1; row >= 0; row-- {\n            if rotated[row][col] == \'#\' {\n                rotated[row][col] = \'.\'\n                rotated[ind][col] = \'#\'\n                ind--\n            } else if rotated[row][col] == \'*\' {\n                ind = row - 1\n            }\n        }\n    }\n\n    return rotated\n}\n```\n\n---\n\n![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style=\'width:250px\'}
['Array', 'Two Pointers', 'C', 'Matrix', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'Dart']