🚀Simulate Gravity and Rotate Box✅| Beats💯
Intuition\n\nTo solve the problem of simulating gravity and rotating the box:\n1. Gravity Simulation: Stones (#) fall downward until stopped by an obstacle (*) 
# Intuition\n\nTo solve the problem of simulating gravity and rotating the box:\n1. **Gravity Simulation**: Stones (`#`) fall downward until stopped by an obstacle (`*`) or the bottom of the box.\n2. **Matrix Rotation**: After simulating gravity for each row, rotate the entire matrix 90 degrees clockwise.\n\nThis involves two main operations: simulating the fall of stones within rows and transforming the matrix to achieve the desired rotation.\n\n# Approach\n\n1. **Simulate Gravity**:    \n    - For each row, treat it as a "fall zone" for stones.\n    - Traverse the row backward to let stones fall down:\n        - Use a pointer to track the current position where a stone can fall.\n        - Obstacles (`*`) stop the movement of stones and reset the pointer.\n2. **Rotate the Matrix**:\n    - After processing all rows, rotate the matrix clockwise:\n        - Transpose the matrix.\n        - Reverse each row of the transposed matrix.\n3. **Return the Result**:\n    - The result is the rotated matrix where all stones have fallen due to gravity.\n\n# Complexity\n\n1. **Time Complexity**: ***O(m\xD7n)***\n    - Gravity simulation for all rows: ***O(m\xD7n)***.\n    - Matrix rotation (transpose + reverse): ***O(m\xD7n)***.\n2. **Space Complexity**: ***O(m\xD7n)***\n    - Space required for the rotated matrix.\n\n# Code \n\n```typescript\nfunction rotateTheBox(box: string[][]): string[][] {\n    const m = box.length;\n    const n = box[0].length;\n\n    // Simulate gravity for each row\n    for (let row = 0; row < m; row++) {\n        let empty = n - 1; // Pointer to where the next stone can fall\n        for (let col = n - 1; col >= 0; col--) {\n            if (box[row][col] === \'#\') {\n                // Move the stone to the last empty spot\n                box[row][col] = \'.\';\n                box[row][empty] = \'#\';\n                empty--;\n            } else if (box[row][col] === \'*\') {\n                // Reset the empty spot pointer at the obstacle\n                empty = col - 1;\n            }\n        }\n    }\n\n    // Rotate the box 90 degrees clockwise\n    const rotated: string[][] = Array.from({ length: n }, () => Array(m).fill(\'.\'));\n    for (let row = 0; row < m; row++) {\n        for (let col = 0; col < n; col++) {\n            rotated[col][m - 1 - row] = box[row][col];\n        }\n    }\n\n    return rotated;\n};\n\n```\n\n---\n\n### Example Walkthrough\n\n#### Example 1\n\n**Input**:\n\n```text\nbox = [["#",".","#"]]\n```\n\n**Steps**:\n\n1. Simulate gravity: \n    - Row 0: Stones fall to the bottom \u2192 `["#","#","."]`.\n2. Rotate clockwise:\n    - Transpose \u2192 `["#"], ["#"], ["."]`.\n**Output**:\n```text\n[["."],\n ["#"],\n ["#"]]\n```\n\n#### Example 2\n\n**Input**:\n\n```text\nbox = [\n    ["#",".","*","."],\n    ["#","#","*","."]\n]\n```\n\n**Steps**:\n\n1. Simulate gravity:    \n    - Row 0: `["#",".","*","."]` \u2192 `[".",".","*","#"]`.\n    - Row 1: `["#","#","*","."]` \u2192 `[".",".","*","#"]`.\n2. Rotate clockwise:\n    - Transpose \u2192 `["#","#"], [".","."], ["*","*"], [".","."]`.\n**Output**:\n```text\n[["#","."],\n ["#","#"],\n ["*","*"],\n [".","."]]\n```\n\n#### Example 3\n\n**Input**:\n\n```text\nbox = [\n    ["#","#","*",".","*","."],\n    ["#","#","#","*",".","."],\n    ["#","#","#",".","#","."]\n]\n```\n\n**Steps**:\n\n1. Simulate gravity:    \n    - Row 0: `["#","#","*",".","*","."]` \u2192 `["#","#","*",".","*","."]` (no change).\n    - Row 1: `["#","#","#","*",".","."]` \u2192 `[".","#","#","*",".","#"]`.\n    - Row 2: `["#","#","#",".","#","."]` \u2192 `[".","#","#",".","#","#"]`.\n2. Rotate clockwise:\n    - Transpose \u2192 Rotate \u2192 Output as below.\n**Output**:\n```text\n[[".","#","#"],\n [".","#","#"],\n ["#","#","*"],\n ["#","*","."],\n ["#",".","*"],\n ["#",".","."]]\n```\n\n---\n\n### Key Insights\n\n- Gravity is simulated independently for each row.\n- Rotating the matrix involves a standard transpose and reverse operation.\n- The approach ensures correctness while maintaining efficiency.
['TypeScript']