Easy

Quiz

#566 Reshape the Matrix

APPROACH

Given an m x n matrix mat and target dimensions r and c, rearrange the elements of mat in row-major order into a new r x c matrix. If the total element count differs (i.e., m * n != r * c), return the original matrix unchanged.

Example 1:

Input: mat = [[1,2],[3,4]], r = 1, c = 4
Output: [[1,2,3,4]]

Example 2:

Input: mat = [[1,2],[3,4]], r = 2, c = 4
Output: [[1,2],[3,4]]
1 of 4
1:00

What is the optimal approach for this problem?