2025-08-02 06:11:31 +0000 UTC

Matrix Cells in Distance Order

Code

class Solution:
    def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:
        res = []
        for row in range(rows):
            for col in range(cols):
                dist = abs(row - rCenter) + abs(col - cCenter)
                res.append((dist, row, col))
        res.sort()
        for i in range(len(res)):
            res[i] = res[i][1:] 
        return res