This article is another part of a series on leet code problems solutions, and it provides a solution to the leet code problem number 566 . This is a Leet code problem named Reshape the Matrix. We will solve it using python and it is the best space and time-optimized solution.
Question 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
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
Approach to solve Reshape the Matrix
To solve this problem we will traverse through the matrix and while traversing we will create an new matrix of given dimensions.
Variables and its usage
We will use four variables here to complete our code:
- i- Variable to check the number of elements to be inserted in a row.
- arr[] – This list is used to store elements and then inserted into new array as a single row.
- new_mat[] – This variable is used to store our new reshaped matrix.
- m – Number of rows of matrix.
- n – Number of columns of matrix
Algorithm
This method performs the following steps for a given input array:
- Initialize variable i with 0.
- Create an empty list arr[].
- Initialize variable m and store the number of rows in it.
- Initialize variable n and store the number of columns in it.
- Now check whether the given elements of the matrix are equal to the elements of the new matrix.
- If yes then go to step 7 otherwise return the old matrix and break.
- Now we traverse the matrix.
- Repeat steps 9 to 16 for each row.
- For each element in a row repeat the step from 10 to.
- Add element in arr.
- Increment i by 1.
- Check whether i is equal to the number of columns of the new matrix.
- If yes then follow steps 14,15 and 16.
- Add arr in the new_matrix variable.
- Initialize arr to blank list.
- Initialize i to 0.
- Return new_mat.
Python Code for Reshape the Matrix
class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
#variable to check number of entries
i=0
# array to store columns
arr=[]
# to store new matrix
new_mat = []
#number of rows
m = len(mat)
#number of columns
n=len(mat[0])
#check wheather both matrix contains same number of elements
if r*c != m*n:
#If not then return the given matrix
return mat
# run for loop for rows
for row in mat:
#run for loop for elements of columns
for elem in row:
#add elem value in arr
arr.append(elem)
#increment entries pointer
i+=1
#check i is equal to columns to be entered
if(i==c):
#if yes then add arr list to new_mat
new_mat.append(arr)
#reinitilize arr to blank list
arr=[]
#reinitilize i to 0
i=0
return new_mat
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(m*n)
Related Posts:
- Find Pivot Index (Leet Code-724)
- Spiral Matrix Problem Solution
- Isomorphic Strings
- Two Sum IV - Input is a BST (Leet Code-653)
- Search in a Binary Search Tree (Leet Code-700)
- Invert Binary Tree (Leet Code-266)
- Symmetric Tree (Leet Code-101)
- Maximum Depth of Binary Tree (Leet Code-104)
- Binary Tree Postorder Traversal (Leet Code-145)
- Binary Tree Inorder Traversal (Leet Code - 94)
- Binary Tree Pre-Order Traversal
- Binary Tree Level Order Traversal (Leet Code-102)
- Valid Parentheses (Leet Code-20)
- Remove Duplicates from Sorted List (Leet Code-83)
- Reverse Linked List (Leet code -206)
- Remove Linked List Elements (Leet Code-203)
- Merge Two Sorted Lists (Leet code-21)
- Ransom Note (Leet code-383)
- First Unique Character in a String (Leet code-387)
- Valid Sudoku (Leet Code-36)
- Search a 2D Matrix (Leet Code-74)
- Linked List Cycle (Leet Code-141)
- Pascal's Triangle (Leet Code - 118)
- Best Time to Buy and Sell Stock (Leet code-121)
- The Intersection of Two Arrays II (Leet Code)
- Binary Search - Leet Code
- Two Sum - Leet Code