Easy to understand 😊
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to reshape the input matrix mat into a new matrix with r rows and c columns
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to reshape the input matrix mat into a new matrix with r rows and c columns. The reshaping operation should be performed only if the total number of elements in the input matrix is equal to the total number of elements in the desired output matrix (i.e., **mat.length * mat[0].length == r * c)**.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **First, a new matrix \'s\' with dimensions \'r\' x \'c\' is created to store the reshaped matrix.**\n2. **Two variables, \'x\' and \'y\', are used to keep track of the current position in the new matrix \'s\'**.\n3. **If the total number of elements in the input matrix is equal to the total number of elements in the desired output matrix, the reshaping is possible. In that case:**\n   - A nested loop is used to iterate through each element of the input matrix.\n   - If the column index \'y\' reaches the desired number of columns \'c\', it means the current row is complete, so \'x\' is incremented, and \'y\' is reset to \'0\'.\n   - The value of the current element in the input matrix \'mat\' is assigned to the corresponding position in the new matrix \'s\'.\n   - The column index \'y\' is incremented.\n4. **If the reshaping is not possible (i.e., the total number of elements does not match), the original matrix \'mat\' is returned unchanged.**\n\n\n# Complexity\n- Time complexity: ***O(m * n)***\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: ***O(r * c)***\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n        int[][] s = new int[r][c];\n        int x = 0;\n        int y = 0;\n        if(mat.length * mat[0].length == r * c){\n            for(int i =0; i<mat.length; i++){\n                for(int j = 0; j<mat[0].length; j++){\n                    if(y==c){\n                        x++;\n                        y = 0;\n                    }\n                    s[x][y] = mat[i][j];\n                    y++;\n                }\n            }\n            return s;\n        }\n        return mat;\n    }\n}\n```\n```python []\nclass Solution:\n    def matrixReshape(self, mat, r, c):\n        s = [[0] * c for _ in range(r)]\n        x, y = 0, 0\n\n        if len(mat) * len(mat[0]) == r * c:\n            for i in range(len(mat)):\n                for j in range(len(mat[0])):\n                    if y == c:\n                        x += 1\n                        y = 0\n                    s[x][y] = mat[i][j]\n                    y += 1\n            return s\n        return mat\n```\n```cpp []\n#include <vector>\n\nclass Solution {\npublic:\n    std::vector<std::vector<int>> matrixReshape(std::vector<std::vector<int>>& mat, int r, int c) {\n        std::vector<std::vector<int>> s(r, std::vector<int>(c, 0));\n        int x = 0, y = 0;\n\n        if (mat.size() * mat[0].size() == r * c) {\n            for (int i = 0; i < mat.size(); ++i) {\n                for (int j = 0; j < mat[0].size(); ++j) {\n                    if (y == c) {\n                        x++;\n                        y = 0;\n                    }\n                    s[x][y] = mat[i][j];\n                    y++;\n                }\n            }\n            return s;\n        }\n        return mat;\n    }\n};\n```
['Python', 'C++', 'Java']