Skip to content

Commit

Permalink
Reshape the Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sukhoverkhov committed Oct 1, 2024
1 parent 5625632 commit ddb6204
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions python/leetcode/Reshape the Matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import List


class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:

rows = len(mat)
columns = len(mat[0])

if rows * columns != r * c:
return mat

result = [[0] * c for _ in range(r)]
ii, jj = 0, 0
for i in range(rows):
for j in range(columns):
result[ii][jj] = mat[i][j]

jj += 1
if jj >= c:
jj = 0
ii += 1

return result


if __name__ == "__main__":
obj = Solution()

mat = [[1, 2, 3, 4]]
r = 2
c = 2
assert obj.matrixReshape(mat, r, c) == [[1, 2], [3, 4]]

mat = [[1, 2], [3, 4]]
r = 2
c = 4
assert obj.matrixReshape(mat, r, c) == [[1, 2], [3, 4]]

mat = [[1, 2], [3, 4]]
r = 1
c = 4
assert obj.matrixReshape(mat, r, c) == [[1, 2, 3, 4]]

0 comments on commit ddb6204

Please sign in to comment.