Java Efficient Solution || Matrix Time Complexity
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves rotating a grid of characters (which can contain empty spaces, obs
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves rotating a grid of characters (which can contain empty spaces, obstacles, and stones) by 90 degrees clockwise. The grid is represented by a 2D character array. The idea is to rotate the grid such that:\n- Gravity Adjustment: Before rotating the box, we need to ensure that all the stones (#) are "fallen" to the bottom of their respective columns.\n- Rotation: After the gravity adjustment, the whole grid needs to be rotated 90 degrees clockwise.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Gravity Adjustment:**\n    - For each row in the grid, process each column to ensure that stones (#) fall to the bottom. Empty spaces (.) or obstacles (*) should not affect the stones\' final positions. \n    - Start from the bottom of each column and move upwards. If a stone is found, it should be shifted down as far as possible, respecting any obstacles (*).\n2. **Rotation:**\n    - After the gravity adjustment, we will rotate the grid 90 degrees clockwise. The transformation from box[i][j] in the original grid to rotated[j][m-1-i] in the rotated grid is straightforward.\n\n# Complexity\n- Time complexity: The time complexity for gravity adjustment is $$O(m * n)$$, where m is the number of rows and n is the number of columns. \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: The space complexity is $$O(m * n)$$ for the rotated grid.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public char[][] rotateTheBox(char[][] box) {\n        int m = box.length;\n        int n = box[0].length;\n        char[][] rotated =new char[n][m];\n        for(int i =m-1;i>=0;i--){\n            int spot = n-1;\n            for(int j = n-1;j>=0;j--){\n                if(box[i][j]==\'*\')\n                    spot = j-1;\n                if(box[i][j]==\'#\'){\n                    char temp = box[i][spot];\n                    box[i][spot] = box[i][j];\n                    box[i][j]= temp;\n                    spot--;\n                }\n            }\n        }\n        for(int i=0,k=rotated[0].length-1;i<box.length;i++,k--){\n            for(int j=0;j<box[0].length;j++){\n                rotated[j][k] = box[i][j];\n            }\n        }\n        return rotated;\n    }\n}\n```
['Java']