[JAVA] O(nm) solution 1 ms, best solution
\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n       int n = mat.length, m = mat[0].length;\n      \n      if(r * c != m * 
```\nclass Solution {\n    public int[][] matrixReshape(int[][] mat, int r, int c) {\n       int n = mat.length, m = mat[0].length;\n      \n      if(r * c != m * n) {\n        return mat;\n      }\n      \n      int result[][] = new int[r][c];\n      for(int i = 0; i < r * c; i ++) {\n        result[i / c][i % c] = mat[i / m][i % m];\n      }\n      return result;\n    }\n}\n```\n# PLEASE UPVOTE IF IT WAS HELPFULL
['Matrix', 'Java']