Matrix Cells in Distance Order

You are given four integers rows, cols, rCenter, and cCenter. There is a rows x cols matrix, and you are on the cell with the coordinates (rCenter, cCenter).

Return the coordinates of all cells in the matrix, sorted by their distance from (rCenter, cCenter) from the smallest distance to the largest distance. You may return the answer in any order that satisfies this condition.

The distance between two cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|.

Example 1
Inputrows = 1, cols = 2, rCenter = 0, cCenter = 0
Output[[0,0],[0,1]]
The distances from (0, 0) to other cells are [0, 1].
Example 2
Inputrows = 2, cols = 2, rCenter = 0, cCenter = 1
Output[[0,1],[0,0],[1,1],[1,0]]
The distances from (0, 1) to other cells are [0, 1, 1, 2], and other orders with the same nondecreasing distances are also accepted.

Constraints

  • 1 <= rows, cols <= 100
  • 0 <= rCenter < rows
  • 0 <= cCenter < cols

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate