Sort Every Concentric Matrix Border Clockwise
Given a nonempty rectangular integer matrix, process each concentric layer independently.
For a layer, list its coordinates clockwise starting at that layer's top-left cell. Sort the values from that border in ascending order, then write them back along the same clockwise coordinate order. Return the transformed matrix.
The innermost layer may be a single row, a single column, or one cell. Every matrix cell belongs to exactly one layer.
Function Signature
def sort_matrix_borders(matrix: list[list[int]]) -> list[list[int]]:
...
Constraints
-
1 <= len(matrix) <= 200
-
1 <= len(matrix[0]) <= 200
-
Every row has the same length.
-
-1_000_000_000 <= matrix[r][c] <= 1_000_000_000
Example
Input:
[[9, 8, 7],
[6, 5, 4],
[3, 2, 1]]
Output:
[[1, 2, 3],
[9, 5, 4],
[8, 7, 6]]
The outer values are written in ascending order along the clockwise path from the top-left cell; the center cell forms its own layer.