Kotlin | Rust
\n\nhttps://youtu.be/ICOKxq-rEUM\n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/809\n\n#### Problem TLDR\n\nRotate matrix and simulate t
![1.webp](https://assets.leetcode.com/users/images/7a37d626-445c-4837-b22b-a1076d4df970_1732354103.3764367.webp)\n\nhttps://youtu.be/ICOKxq-rEUM\n\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/809\n\n#### Problem TLDR\n\nRotate matrix and simulate the fall #medium #matrix\n\n#### Intuition\n\nThis problem is all about careful implementation.\nWe can simulate fall first, then rotate result, or do this in a single step.\n\n#### Approach\n\n* it is simpler to simulate fall by only writing `*` and `#` in a new object with an explicit pointer `k` instead of doing this in-place\n* `y` coordinate will change the direction\n* a joke solution with converting to string and sorting possible\n\n#### Complexity\n\n- Time complexity:\n$$O(nm)$$\n\n- Space complexity:\n$$O(nm)$$\n\n#### Code\n\n```kotlin []\n\n    fun rotateTheBox(box: Array<CharArray>): Array<CharArray> {\n        val res = Array(box[0].size) { CharArray(box.size) { \'.\' }}\n        for ((i, r) in box.withIndex()) {\n            var k = r.lastIndex\n            for (j in k downTo 0) if (r[j] != \'.\') {\n                if (r[j] == \'*\') k = j\n                res[k--][box.lastIndex - i] = r[j]\n            }\n        }\n        return res\n    }\n\n```\n```rust []\n\n    pub fn rotate_the_box(b: Vec<Vec<char>>) -> Vec<Vec<char>> {\n        let mut res = vec![vec![\'.\'; b.len()]; b[0].len()];\n        for i in 0..b.len() {\n            let mut k = res.len() - 1;\n            for j in (0..=k).rev() { if b[i][j] != \'.\' {\n                if b[i][j] == \'*\' { k = j }\n                res[k][b.len() - 1 - i] = b[i][j]; k -= 1\n            }}\n        }; res\n    }\n\n```\n```c++ []\n\n    vector<vector<char>> rotateTheBox(vector<vector<char>>& b) {\n        vector<vector<char>> r(b[0].size(), vector<char>(b.size(), \'.\'));\n        for (int i = 0, n = b.size(), m = r.size(); i < n; ++i) \n            for (int k = m - 1, j = k; j >= 0; --j) if (b[i][j] != \'.\')\n                r[(k = b[i][j] == \'*\' ? j : k)--][n - 1 - i] = b[i][j];\n        return r;\n    }\n\n```\n```kotlin []\n\n    fun rotateTheBox(box: Array<CharArray>) =\n        box.map { r ->\n            r.joinToString("").split(\'*\')\n            .map { it.toCharArray().sorted().reversed().joinToString("") }\n            .joinToString("*").toCharArray()\n        }.run { List(box[0].size) { x -> List(box.size) { this[box.lastIndex - it][x] }}}\n\n\n```
['Matrix', 'C++', 'Rust', 'Kotlin']