Rotate & falling stones||145ms Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nRotate the matrix  & consider the gravity.\nC, C++ & Python are made.\n# Approach\n Des
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRotate the matrix  & consider the gravity.\nC, C++ & Python are made.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Declare `cxr` matrix `rotate` filled with \'.\'\n2. Use a nested double for loop to proceed; initialze `bottom=c-1`\n3. In the inner loop transverse backwards do the following\n```\nif (box[i][j]==\'#\'){// a stone falling down\n    rotate[bottom][r-1-i]=\'#\';// rotate & stone goes to the bottom\n    bottom--; // update\n}\nelse if (box[i][j]==\'*\'){// a support\n    rotate[j][r-1-i]=\'*\';// just rotate\n    bottom=j-1; // update\n}\n```\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$O(rc)$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nExtra:$O(1)$  not count the returning space $O(rc)$ for `rotate`\n# Code||C++ 145ms Beats 100.00%\n```cpp []\nclass Solution {\npublic:\n    static vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {\n        const int r=box.size(), c=box[0].size();\n        vector<vector<char>> rotate(c, vector<char>(r, \'.\'));\n        for(int i=0; i<r; i++){\n            int bottom=c-1;\n            for(int j=c-1; j>=0; j--){\n                if (box[i][j]==\'#\'){\n                    rotate[bottom][r-1-i]=\'#\';\n                    bottom--;\n                }\n                else if (box[i][j]==\'*\'){\n                    rotate[j][r-1-i]=\'*\';\n                    bottom=j-1;\n                }\n            }\n        }\n        return rotate;\n    }\n};\n\n\n\nauto init = []()\n{ \n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n```Python []\nclass Solution:\n    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:\n        r, c=len(box), len(box[0])\n        rotate=[[\'.\']*r for _ in range(c)]\n        for i, row in enumerate(box):\n            bottom=c-1\n            for j in range(c-1, -1, -1):\n                if row[j]==\'#\':\n                    rotate[bottom][r-1-i]=\'#\'\n                    bottom-=1\n                elif row[j]==\'*\':\n                    rotate[j][r-1-i]=\'*\'\n                    bottom=j-1\n        return rotate\n        \n```\n```C []\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume\n * caller calls free().\n */\n#pragma GCC optimize("O3", "unroll-loops")\nchar** rotateTheBox(char** box, int r, int* boxColSize, int* returnSize,\n                    int** returnColumnSizes) {\n    int c = *boxColSize;\n    *returnSize = c;\n    *returnColumnSizes = (int*)malloc(c * sizeof(int));\n    char** rotate = (char**)malloc(c * sizeof(char*));\n    for (register i = 0; i < c; i++) {\n        rotate[i] = (char*)malloc(r * sizeof(char));\n        memset(rotate[i], \'.\', r);\n        (*returnColumnSizes)[i] = r;\n    }\n    for (register int i = 0; i < r; i++) {\n        int bottom = c - 1;\n        char* row = box[i];\n        for ( register int j = c - 1; j >= 0; j--) {\n            if (row[j] == \'#\') {\n                rotate[bottom][r - 1 - i] = \'#\';\n                bottom--;\n            } else if (row[j] == \'*\') {\n                rotate[j][r - 1 - i] = \'*\';\n                bottom = j - 1;\n            }\n        }\n    }\n    return rotate;\n}\n```
['Array', 'C', 'Matrix', 'C++', 'Python3']