Advertisement
jorupp

https://leetcode.com/problems/reshape-the-matrix

Oct 24th, 2023
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. function matrixReshape(mat: number[][], r: number, c: number): number[][] {
  2. const total = mat.length * mat[0].length;
  3. if (total !== r*c) return mat;
  4.  
  5. return Array.from({ length: r }).map((_, i) =>
  6. Array.from({ length: c}).map((__, j) => {
  7. const ix = c*i + j;
  8. const oi = Math.floor(ix / mat[0].length);
  9. const oj = ix % mat[0].length;
  10. return mat[oi][oj];
  11. })
  12. );
  13. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement