Original title: how do i put the elements back in the matrix to return it sorted [duplicate]
class Solution: def sortMatrix(self, grid: List[List[int]]) -> List[List[int]]:
maxRows = len(grid)
maxColumns = len(grid[0])
backWardsDiags = [[] for _ in range(maxRows * maxColumns - 1)]
aux = [[] for _ in range(maxRows * maxColumns - 1)]
minBdiag = -maxRows + 1
for row in range (maxRows):
for column in range (maxColumns):
backWardsDiags[row - column - minBdiag].append(grid[row][column])
if row > column:
for x in backWardsDiags:
x.sort()
else:
for y in backWardsDiags:
y.sort(reverse = True)
for i in range(maxRows):
for j in range(maxColumns):
key = i - j - minBdiag
aux[i][j] = backWardsDiags[key].pop(0)
return aux
I’m getting this error: IndexError: list assignment index out of range