✅ Video | Short and Simple
https://youtu.be/LZr1w0LVzFw?si=2AZkoOp_fMXa81Ck\n\n# #1 NeetCode Solution From Video (2nd)\n\n\n\n\npython3 []\nclass Solution:\n    def rotateTheBox(self, box
https://youtu.be/LZr1w0LVzFw?si=2AZkoOp_fMXa81Ck\n\n# #1 NeetCode Solution From Video (2nd)\n\n\n\n\n```python3 []\nclass Solution:\n    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:\n        ROWS, COLS = len(box), len(box[0])\n\n        res = [["."] * ROWS for _ in range(COLS)]\n\n        for r in range(ROWS):\n            i = COLS - 1\n            for c in reversed(range(COLS)):\n                if box[r][c] == "#":\n                    res[i][ROWS - r - 1] = "#"\n                    i -= 1\n                elif box[r][c] == "*":\n                    res[c][ROWS - r - 1] = "*"\n                    i = c - 1\n\n        return res\n```\n```cpp []\nclass Solution {\npublic:\n    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n        int ROWS = box.size();\n        int COLS = box[0].size();\n        \n        vector<vector<char>> res(COLS, vector<char>(ROWS, \'.\'));\n        \n        for (int r = 0; r < ROWS; r++) {\n            int i = COLS - 1;\n            for (int c = COLS - 1; c >= 0; c--) {\n                if (box[r][c] == \'#\') {\n                    res[i][ROWS - r - 1] = \'#\';\n                    i--;\n                }\n                else if (box[r][c] == \'*\') {\n                    res[c][ROWS - r - 1] = \'*\';\n                    i = c - 1;\n                }\n            }\n        }\n        \n        return res;\n    }\n};\n```\n```java []\nclass Solution {\n    public char[][] rotateTheBox(char[][] box) {\n        int ROWS = box.length;\n        int COLS = box[0].length;\n        \n        char[][] res = new char[COLS][ROWS];\n        for(char[] row : res) {\n            Arrays.fill(row, \'.\');\n        }\n        \n        for (int r = 0; r < ROWS; r++) {\n            int i = COLS - 1;\n            for (int c = COLS - 1; c >= 0; c--) {\n                if (box[r][c] == \'#\') {\n                    res[i][ROWS - r - 1] = \'#\';\n                    i--;\n                } else if (box[r][c] == \'*\') {\n                    res[c][ROWS - r - 1] = \'*\';\n                    i = c - 1;\n                }\n            }\n        }\n        return res;\n    }\n}\n```\n```csharp []\npublic class Solution {\n    public char[][] RotateTheBox(char[][] box) {\n        int ROWS = box.Length;\n        int COLS = box[0].Length;\n        \n        char[][] res = new char[COLS][];\n        for(int i = 0; i < COLS; i++) {\n            res[i] = new char[ROWS];\n            Array.Fill(res[i], \'.\');\n        }\n        \n        for (int r = 0; r < ROWS; r++) {\n            int i = COLS - 1;\n            for (int c = COLS - 1; c >= 0; c--) {\n                if (box[r][c] == \'#\') {\n                    res[i][ROWS - r - 1] = \'#\';\n                    i--;\n                } else if (box[r][c] == \'*\') {\n                    res[c][ROWS - r - 1] = \'*\';\n                    i = c - 1;\n                }\n            }\n        }\n        return res;\n    }\n}\n```\n```golang []\nfunc rotateTheBox(box [][]byte) [][]byte {\n    ROWS := len(box)\n    COLS := len(box[0])\n    \n    res := make([][]byte, COLS)\n    for i := range res {\n        res[i] = make([]byte, ROWS)\n        for j := range res[i] {\n            res[i][j] = \'.\'\n        }\n    }\n    \n    for r := 0; r < ROWS; r++ {\n        i := COLS - 1\n        for c := COLS - 1; c >= 0; c-- {\n            if box[r][c] == \'#\' {\n                res[i][ROWS - r - 1] = \'#\'\n                i--\n            } else if box[r][c] == \'*\' {\n                res[c][ROWS - r - 1] = \'*\'\n                i = c - 1\n            }\n        }\n    }\n    return res\n}\n```\n```swift []\nclass Solution {\n    func rotateTheBox(_ box: [[Character]]) -> [[Character]] {\n        let ROWS = box.count\n        let COLS = box[0].count\n        \n        var res = Array(repeating: Array(repeating: Character("."), count: ROWS), count: COLS)\n        \n        for r in 0..<ROWS {\n            var i = COLS - 1\n            for c in stride(from: COLS - 1, through: 0, by: -1) {\n                if box[r][c] == "#" {\n                    res[i][ROWS - r - 1] = "#"\n                    i -= 1\n                } else if box[r][c] == "*" {\n                    res[c][ROWS - r - 1] = "*"\n                    i = c - 1\n                }\n            }\n        }\n        return res\n    }\n}\n```\n```javascript [JS]\n// JavaScript\n\nfunction rotateTheBox(box) {\n    const ROWS = box.length;\n    const COLS = box[0].length;\n    \n    const res = Array(COLS).fill(0)\n        .map(() => Array(ROWS).fill(\'.\'));\n    \n    for (let r = 0; r < ROWS; r++) {\n        let i = COLS - 1;\n        for (let c = COLS - 1; c >= 0; c--) {\n            if (box[r][c] === \'#\') {\n                res[i][ROWS - r - 1] = \'#\';\n                i--;\n            } else if (box[r][c] === \'*\') {\n                res[c][ROWS - r - 1] = \'*\';\n                i = c - 1;\n            }\n        }\n    }\n    return res;\n}\n```\n```typescript [TS]\n// TypeScript\n\nfunction rotateTheBox(box: string[][]): string[][] {\n    const ROWS = box.length;\n    const COLS = box[0].length;\n    \n    const res: string[][] = Array(COLS).fill(0)\n        .map(() => Array(ROWS).fill(\'.\'));\n    \n    for (let r = 0; r < ROWS; r++) {\n        let i = COLS - 1;\n        for (let c = COLS - 1; c >= 0; c--) {\n            if (box[r][c] === \'#\') {\n                res[i][ROWS - r - 1] = \'#\';\n                i--;\n            } else if (box[r][c] === \'*\') {\n                res[c][ROWS - r - 1] = \'*\';\n                i = c - 1;\n            }\n        }\n    }\n    return res;\n}\n```\n```rust []\nimpl Solution {\n    pub fn rotate_the_box(grid: Vec<Vec<char>>) -> Vec<Vec<char>> {\n        let rows = grid.len();\n        let cols = grid[0].len();\n        \n        let mut res = vec![vec![\'.\'; rows]; cols];\n        \n        for r in 0..rows {\n            let mut i = cols - 1;\n            for c in (0..cols).rev() {\n                if grid[r][c] == \'#\' {\n                    res[i][rows - r - 1] = \'#\';\n                    i = i.saturating_sub(1);\n                } else if grid[r][c] == \'*\' {\n                    res[c][rows - r - 1] = \'*\';\n                    i = c.saturating_sub(1);\n                }\n            }\n        }\n        res\n    }\n}\n```\n```ruby []\ndef rotate_the_box(box)\n    rows = box.length\n    cols = box[0].length\n    \n    res = Array.new(cols) { Array.new(rows, \'.\') }\n    \n    (0...rows).each do |r|\n        i = cols - 1\n        (cols-1).downto(0) do |c|\n            if box[r][c] == \'#\'\n                res[i][rows - r - 1] = \'#\'\n                i -= 1\n            elsif box[r][c] == \'*\'\n                res[c][rows - r - 1] = \'*\'\n                i = c - 1\n            end\n        end\n    end\n    res\nend\n```\n\n- Complexy: Time O(m * n) & Space O(m * n)\n\n\n\n\n# #2 Simple approach \n```python3 []\nclass Solution:\n    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:\n        for row in box:\n            dropPos = len(row) - 1  \n            \n            for currPos in range(len(row)-1, -1, -1):\n                if row[currPos] == "*":  \n                    dropPos = currPos - 1\n                elif row[currPos] == "#":  \n                    row[dropPos], row[currPos] = row[currPos], row[dropPos]\n                    dropPos -= 1\n\n        rotatedBox = zip(*box[::-1])\n        return rotatedBox\n```\n```cpp []\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        for (auto& row : box) {\n            int dropPos = n - 1;\n            \n            for (int currPos = n - 1; currPos >= 0; currPos--) {\n                if (row[currPos] == \'*\') {\n                    dropPos = currPos - 1;\n                }\n                else if (row[currPos] == \'#\') {\n                    swap(row[dropPos], row[currPos]);\n                    dropPos--;\n                }\n            }\n        }\n        \n        vector<vector<char>> rotatedBox(n, vector<char>(m));\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                rotatedBox[j][m - 1 - i] = box[i][j];\n            }\n        }\n        \n        return rotatedBox;\n    }\n};\n```\n```java []\nclass Solution {\n    public char[][] rotateTheBox(char[][] box) {\n        int m = box.length;\n        int n = box[0].length;\n        \n        for (char[] row : box) {\n            int dropPos = n - 1;\n            \n            for (int currPos = n - 1; currPos >= 0; currPos--) {\n                if (row[currPos] == \'*\') {\n                    dropPos = currPos - 1;\n                }\n                else if (row[currPos] == \'#\') {\n                    char temp = row[dropPos];\n                    row[dropPos] = row[currPos];\n                    row[currPos] = temp;\n                    dropPos--;\n                }\n            }\n        }\n        \n        char[][] rotatedBox = new char[n][m];\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                rotatedBox[j][m - 1 - i] = box[i][j];\n            }\n        }\n        \n        return rotatedBox;\n    }\n}\n```\n```csharp []\npublic class Solution {\n    public char[][] RotateTheBox(char[][] box) {\n        int m = box.Length;\n        int n = box[0].Length;\n        \n        foreach (char[] row in box) {\n            int dropPos = n - 1;\n            \n            for (int currPos = n - 1; currPos >= 0; currPos--) {\n                if (row[currPos] == \'*\') {\n                    dropPos = currPos - 1;\n                }\n                else if (row[currPos] == \'#\') {\n                    char temp = row[dropPos];\n                    row[dropPos] = row[currPos];\n                    row[currPos] = temp;\n                    dropPos--;\n                }\n            }\n        }\n        \n        char[][] rotatedBox = new char[n][];\n        for (int i = 0; i < n; i++) {\n            rotatedBox[i] = new char[m];\n        }\n        \n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                rotatedBox[j][m - 1 - i] = box[i][j];\n            }\n        }\n        \n        return rotatedBox;\n    }\n}\n```\n```golang []\nfunc rotateTheBox(box [][]byte) [][]byte {\n    m, n := len(box), len(box[0])\n    \n    for _, row := range box {\n        dropPos := n - 1\n        \n        for currPos := n - 1; currPos >= 0; currPos-- {\n            if row[currPos] == \'*\' {\n                dropPos = currPos - 1\n            } else if row[currPos] == \'#\' {\n                row[dropPos], row[currPos] = row[currPos], row[dropPos]\n                dropPos--\n            }\n        }\n    }\n    \n    rotatedBox := make([][]byte, n)\n    for i := range rotatedBox {\n        rotatedBox[i] = make([]byte, m)\n    }\n    \n    for i := 0; i < m; i++ {\n        for j := 0; j < n; j++ {\n            rotatedBox[j][m-1-i] = box[i][j]\n        }\n    }\n    \n    return rotatedBox\n}\n```\n```swift []\nclass Solution {\n    func rotateTheBox(_ box: [[Character]]) -> [[Character]] {\n        let m = box.count\n        let n = box[0].count\n        var mutableBox = box\n        \n        for i in 0..<m {\n            var dropPos = n - 1\n            \n            for currPos in stride(from: n-1, through: 0, by: -1) {\n                if mutableBox[i][currPos] == "*" {\n                    dropPos = currPos - 1\n                } else if mutableBox[i][currPos] == "#" {\n                    let temp = mutableBox[i][dropPos]\n                    mutableBox[i][dropPos] = mutableBox[i][currPos]\n                    mutableBox[i][currPos] = temp\n                    dropPos -= 1\n                }\n            }\n        }\n        \n        var rotatedBox = Array(repeating: Array(repeating: Character("."), count: m), count: n)\n        for i in 0..<m {\n            for j in 0..<n {\n                rotatedBox[j][m-1-i] = mutableBox[i][j]\n            }\n        }\n        \n        return rotatedBox\n    }\n}\n```\n```javascript [JS]\n// JavaScript\n\nvar rotateTheBox = function(box) {\n    for (let row of box) {\n        let start = row.length - 1;\n        for (let i = row.length - 1; i >= 0; i--) {\n            if (row[i] === \'*\') {\n                start = i - 1;\n            } else if (row[i] === \'#\') {\n                [row[start], row[i]] = [row[i], row[start]];\n                start--;\n            }\n        }\n    }\n    \n    const result = Array(box[0].length).fill()\n        .map(() => Array(box.length).fill(\'.\'));\n    \n    for (let i = 0; i < box.length; i++) {\n        for (let j = 0; j < box[0].length; j++) {\n            result[j][box.length - 1 - i] = box[i][j];\n        }\n    }\n    \n    return result;\n};\n```\n```typescript [TS]\n// TypeScript\n\nfunction rotateTheBox(box: string[][]): string[][] {\n    const m = box.length;\n    const n = box[0].length;\n    \n    for (const row of box) {\n        let dropPos = n - 1;\n        \n        for (let currPos = n - 1; currPos >= 0; currPos--) {\n            if (row[currPos] === \'*\') {\n                dropPos = currPos - 1;\n            } else if (row[currPos] === \'#\') {\n                [row[dropPos], row[currPos]] = [row[currPos], row[dropPos]];\n                dropPos--;\n            }\n        }\n    }\n    \n    const rotatedBox: string[][] = Array(n).fill(0).map(() => Array(m).fill(\'.\'));\n    \n    for (let i = 0; i < m; i++) {\n        for (let j = 0; j < n; j++) {\n            rotatedBox[j][m-1-i] = box[i][j];\n        }\n    }\n    \n    return rotatedBox;\n}\n```\n```rust []\nimpl Solution {\n    pub fn rotate_the_box(box_: Vec<Vec<char>>) -> Vec<Vec<char>> {\n        let mut box_ = box_;\n        for row in box_.iter_mut() {\n            let mut drop_pos = row.len() - 1;\n            for curr_pos in (0..row.len()).rev() {\n                if row[curr_pos] == \'*\' {\n                    drop_pos = curr_pos.saturating_sub(1);\n                } else if row[curr_pos] == \'#\' {\n                    row.swap(drop_pos, curr_pos);\n                    drop_pos = drop_pos.saturating_sub(1);\n                }\n            }\n        }\n        \n        let mut rotated_box = vec![vec![\'.\'; box_.len()]; box_[0].len()];\n        for i in 0..box_.len() {\n            for j in 0..box_[0].len() {\n                rotated_box[j][box_.len() - 1 - i] = box_[i][j];\n            }\n        }\n        rotated_box\n    }\n}\n```\n```ruby []\ndef rotate_the_box(box)\n    m, n = box.length, box[0].length\n    \n    box.each do |row|\n        drop_pos = n - 1\n        \n        (n-1).downto(0) do |curr_pos|\n            if row[curr_pos] == \'*\'\n                drop_pos = curr_pos - 1\n            elsif row[curr_pos] == \'#\'\n                row[drop_pos], row[curr_pos] = row[curr_pos], row[drop_pos]\n                drop_pos -= 1\n            end\n        end\n    end\n    \n    rotated_box = Array.new(n) { Array.new(m, \'.\') }\n    \n    m.times do |i|\n        n.times do |j|\n            rotated_box[j][m-1-i] = box[i][j]\n        end\n    end\n    \n    rotated_box\nend\n```\n- Complexy: Time O(m * n) & Space O(m * n)\n\n\n\n\n\n\n## #2 approach step by step\n\n---\n\n1. Gravity: Start on the right, For each row, \n```\nfor row in box:\n    dropPos = len(row) - 1  \n```\n\n2. Stone movement, scan from right to left\n\n```\nfor currPos in range(len(row)-1, -1, -1):\n```\n\n3. Stone Movement, when hit some opstacle > reset position\n\n```\nif row[currPos] == "*":\n    dropPos = currPos - 1\n```\n\n4. faind stone  "#" > swap to drop position > Update drop pos for next stone\n\n```\nelif row[currPos] == "#":\n    row[dropPos], row[currPos] = row[currPos], row[dropPos]\n    dropPos -= 1\n```\n\n\n5. 90\' clock rotation, reverse rows\n\n```\nrotatedBox = zip(*box[::-1])\n```\n\nDone.\n\n\n---\n\n\n\n\n![image.png](https://assets.leetcode.com/users/images/edf62693-4a89-453f-ada5-a9cc2f038acb_1732321223.9930787.png)\n\n![image.png](https://assets.leetcode.com/users/images/b08fe5cc-9192-460f-9e18-2ea70ffa33a0_1732321156.111628.png)\n
['Swift', 'C++', 'Java', 'Go', 'TypeScript', 'Python3', 'Rust', 'Ruby', 'JavaScript', 'C#']