Memory beats 95.4% of Java
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n        if (r * c != mat.length * mat[0].length){\n            return mat;\n        }\n        int ans[][] = new int[r][c];\n        ArrayList<Integer> arr = new ArrayList<>();\n        for (int[] i: mat){\n            for (int j: i){\n                arr.add(j);\n            }\n        }\n        int a = 0;\n        for (int i = 0; i < r; i++){\n            for (int j = 0; j < c; j++){\n                ans[i][j] = arr.get(a);\n                a++;\n            }\n        }\n        return ans;\n    }\n}\n```
['Java']