✅ easy video solution and explanation with short implementation | Simulation C++
Intuition\n Describe your first thoughts on how to solve this problem. \n- The first thing we can observe in the matrix we have is every leaf will slide to the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The first thing we can observe in the matrix we have is every leaf will slide to the right till it founds a blocked wall on rotating 90 degrees.\n- Then what we can do is to assume the right end of every row as ground and check the blockage point and start filling the leaf to next vacant space it has. \n- you can check out the video here :\nhttps://youtu.be/cheBNTXMVl4\n# Approach  \n1. **Simulate the sliding**:  \n   - For each row in the box, treat the rightmost end of the row as the ground.  \n   - Track the next vacant position from the right, and let stones slide into these positions, skipping over walls.  \n2. **Rotate the box**:  \n   - After all stones have been slid into place, construct the rotated box by mapping rows of the original box into columns of the new matrix in a clockwise rotation pattern.\n3. I would suggest to make a matrix of 2 x 3 and try to rotate it clockwise 90 degree with notations such as (i,j) you will get the patters as `new(j,n - 1 - i) = old(i,j)`; \n\n# Complexity  \n- **Time Complexity**:  \n  - Sliding stones in each row takes $$(O(m)$$, and there are `n` rows, so the sliding step is $$(O(n * m)$$.  \n  - Rotating the box involves creating a new matrix and filling it, also \\$$(O(n * m)$$.  \n  - Overall complexity: $$(O(n * m)$$.  \n\n- **Space Complexity**:  \n  - The additional space required is for the rotated matrix, which is $$(O(n * m)$$.  \n  - Overall complexity: $$(O(n * m)$$.  \n\n# Code  \n```cpp []\nclass Solution {\npublic:\n    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n        int n = box.size(), m = box[0].size();\n        for (int i = 0; i < box.size(); ++i) {\n            int next = m - 1;\n            for (int j = m - 1; j >= 0; --j) {\n                if (box[i][j] == \'*\') {\n                    next = j - 1;\n                } else if (box[i][j] == \'#\') {\n                    swap(box[i][j], box[i][next]);\n                    next--;\n                }\n            }   \n        }\n        vector<vector<char>> newMatrix(m, vector<char>(n));\n        for (int i = 0; i < n; ++i) {\n            for (int j = 0; j < m; ++j) {\n                newMatrix[j][n - i - 1] = box[i][j];\n            }\n        }\n        return newMatrix;\n    }\n};\n```\n```python []\nclass Solution:\n    def rotateTheBox(self, box: list[list[str]]) -> list[list[str]]:\n        n, m = len(box), len(box[0])\n        \n        # Apply gravity\n        for i in range(n):\n            empty = m - 1  # Rightmost empty cell\n            for j in range(m - 1, -1, -1):\n                if box[i][j] == \'*\':\n                    empty = j - 1  # Reset the empty pointer after obstacle\n                elif box[i][j] == \'#\':\n                    box[i][j], box[i][empty] = \'.\', \'#\'  # Move stone to the empty spot\n                    empty -= 1\n        \n        # Rotate the box 90 degrees clockwise\n        rotated = [[None] * n for _ in range(m)]\n        for i in range(n):\n            for j in range(m):\n                rotated[j][n - 1 - i] = box[i][j]\n        \n        return rotated\n\n```\n```Java []\nclass Solution {\n    public char[][] rotateTheBox(char[][] box) {\n        int n = box.length, m = box[0].length;\n        \n        // Apply gravity\n        for (int i = 0; i < n; i++) {\n            int empty = m - 1; // Rightmost empty cell\n            for (int j = m - 1; j >= 0; j--) {\n                if (box[i][j] == \'*\') {\n                    empty = j - 1; // Reset empty pointer after obstacle\n                } else if (box[i][j] == \'#\') {\n                    char temp = box[i][j];\n                    box[i][j] = \'.\';\n                    box[i][empty] = temp;\n                    empty--;\n                }\n            }\n        }\n        \n        // Rotate the box 90 degrees clockwise\n        char[][] rotated = new char[m][n];\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < m; j++) {\n                rotated[j][n - 1 - i] = box[i][j];\n            }\n        }\n        \n        return rotated;\n    }\n}\n\n```\n\n
['Array', 'Two Pointers', 'Simulation', 'Python', 'C++', 'Java']