566. Reshape the Matrix LeetCode Solution

In this guide, you will get 566. Reshape the Matrix LeetCode Solution with the best time and space complexity. The solution to Reshape the Matrix problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Reshape the Matrix solution in C++
  4. Reshape the Matrix solution in Java
  5. Reshape the Matrix solution in Python
  6. Additional Resources
566. Reshape the Matrix LeetCode Solution image

Problem Statement of Reshape the Matrix

In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.
You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix.
The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

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]]

Constraints:

m == mat.length
n == mat[i].length
1 <= m, n <= 100
-1000 <= mat[i][j] <= 1000
1 <= r, c <= 300

Complexity Analysis

  • Time Complexity: O(rc)
  • Space Complexity: O(rc)

566. Reshape the Matrix LeetCode Solution in C++

class Solution {
 public:
  vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
    if (nums.empty() || r * c != nums.size() * nums[0].size())
      return nums;

    vector<vector<int>> ans(r, vector<int>(c));
    int k = 0;

    for (const vector<int>& row : nums)
      for (const int num : row) {
        ans[k / c][k % c] = num;
        ++k;
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

566. Reshape the Matrix LeetCode Solution in Java

class Solution {
  public int[][] matrixReshape(int[][] nums, int r, int c) {
    if (nums.length == 0 || r * c != nums.length * nums[0].length)
      return nums;

    int[][] ans = new int[r][c];
    int k = 0;

    for (int[] row : nums)
      for (final int num : row) {
        ans[k / c][k % c] = num;
        ++k;
      }

    return ans;
  }
}
// code provided by PROGIEZ

566. Reshape the Matrix LeetCode Solution in Python

class Solution:
  def matrixReshape(self, nums: list[list[int]],
                    r: int, c: int) -> list[list[int]]:
    if nums == [] or r * c != len(nums) * len(nums[0]):
      return nums

    ans = [[0 for j in range(c)] for i in range(r)]
    k = 0

    for row in nums:
      for num in row:
        ans[k // c][k % c] = num
        k += 1

    return ans
# code by PROGIEZ

Additional Resources

See also  2338. Count the Number of Ideal Arrays LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.