✅ Reshape Matrix | Python
Approach:\n\n1) At first we will chech weather the given dimension matrix can be created or not. (i.e mn == rc)\n2) We will be storing all the matrix elements i
### ***Approach***:\n\n1) At first we will chech weather the given dimension matrix can be created or not. ***(i.e mn == rc)***\n2) We will be **storing all** the matrix elements in a **1-d array** .\n3) We will initilize a empty matrix of given size (r,c) .\n4) We will Fill the initilize matrix row wise from that 1-d array\n\n### ***Code***\n```\nclass Solution:\n    def matrixReshape(self, mat: list[list[int]], r: int, c: int) -> list[list[int]]:\n        m = len(mat)\n        n = len(mat[0])\n\t\t# Check weather the matrix can be made by using the given r,c values.\n        if m*n != r*c:\n            return mat\n\t\t# Storing in 1-d Array.\n        arr = [0 for i in range(m*n)]\n        count=0\n        for i in range(m):\n            for j in range(n):\n                arr[count] = mat[i][j]\n                count+=1\n\t\t# Initilize a new Matrix with given Dimensions\n        new_matrix = [[0 for i in range(c)] for j in range(r)]\n        count =0\n        for i in range(r):\n            for j in range(c):\n                new_matrix[i][j]=arr[count]\n                count+=1\n        return new_matrix\n```\n\n***Time Complexity*** : O(RxC) \n***Space Complexity*** : O(RxC) where R,C are Row and Column .\n\nNote: This might not the be the best or the only approach , this is just my implementation, and I wanted to make it as simple as possible.\n\nIf you like it Please **Upvote** it.
['Python']