2025-08-02 06:15:14 +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):
                res.append((row, col))
        def key(point: tuple[int, int]) -> int:
            row, col = point
            return abs(row - rCenter) + abs(col - cCenter)
        res.sort(key=key) 
        return res