grid with m rows of n cells. They have to be moved into a tray of size rows × cols while keeping their order: samples are taken row by row from top to bottom, left to right within a row, and fill the new tray in that same order.
If the new tray has exactly as many cells as there are samples, return the new tray. Otherwise the move is impossible, so return the original grid unchanged.grid=[[5, 1, 8], [2, 0, 4]], rows=3, cols=2 → [[5, 1], [8, 2], [0, 4]]
Reading row by row gives 5, 1, 8, 2, 0, 4, which are then laid out two per row.
grid=[[5, 1, 8], [2, 0, 4]], rows=4, cols=2 → [[5, 1, 8], [2, 0, 4]]
That needs 8 cells but there are only 6 numbers, so the original grid comes back unchanged.
grid=[[7], [-3], [9]], rows=1, cols=3 → [[7, -3, 9]]
The column turns into a single row.
m, n ≤ 100 (m = grid.length, n = grid[i].length)grid[i][j] ≤ 1000rows, cols ≤ 300grid=[[5, 1, 8], [2, 0, 4]], rows=3, cols=2
[[5, 1], [8, 2], [0, 4]]
Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.