Given an m x n grid matrix and two positive integers r and c, return a new r x c grid containing the same elements in the same row-major order (read row by row, left to right). If the reshape is impossible because r * c != m * n, return the original matrix.
Examples
Input: matrix = [[1, 2], [3, 4]], r = 1, c = 4
Output: [[1, 2, 3, 4]]
Input: matrix = [[1, 2], [3, 4]], r = 2, c = 4
Output: [[1, 2], [3, 4]]
Explanation: 2 x 4 = 8 cells cannot hold 4 elements, so the input is returned.
Constraints
1 <= m, n, r, c <= 300- Target: O(m * n) time.
Goals
- Check that a reshape preserves the number of cells
- Map a flat position to new coordinates with divmod
- Return the original grid untouched when the reshape is impossible