dsa · easy
Reshape the Matrix
mat is an m × n grid. Reshape it into r rows and c columns, filling the new grid in **row-major** order (left to right, top to bottom) from the same order of cells in mat.
Arguments
mat— m by n grid of integers to reshaper— requested number of rowsc— requested number of columns
If r * c is not equal to m * n, the reshape is impossible: return mat unchanged.
Example
mat = [[1, 2], [3, 4]], r = 1, c = 4.
Four cells in reading order 1, 2, 3, 4 fill a single row → [[1, 2, 3, 4]].
The same mat with r = 2, c = 4 has 2 * 4 = 8 ≠ 4 cells, so return [[1, 2], [3, 4]].
Constraints
1 <= m, n <= 100 1 <= r, c <= 300 -1000 <= mat[i][j] <= 1000 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [[1, 2], [3, 4]] 1 4 Expected: [[1,2,3,4]]
Example 2
Input: [[1, 2], [3, 4]] 2 4 Expected: [[1,2],[3,4]]