[Python] `how do i put the elements back in the matrix to return it sorted

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    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[j] = backWardsDiags[key].pop(0)



    return aux


    I'm getting this error:

    IndexError: list assignment index out of range

    Continue reading...

Compartilhe esta Página