← 返回 capitalone 的题目列表Sort Each Matrix Border and Fill Clockwise
类型:online_judge
Given an n x m integer matrix matrix, divide it into concentric borders/layers:
Layer 0 is the outer border.
Layer 1 is the next border after removing the outer one.
And so on.
For each border:
Extract all elements on that border in clockwise order, starting from the top-left cell of that layer.
Rearrange the extracted elements in ascending order.
Fill them back into the same border clockwise, again starting from the top-left cell of that layer.
Return the resulting matrix.
Note: The original post only said “rearrange” and did not specify the exact rule. This version completes the common CodeSignal variant as “sort ascending”.
Input Format
n m
matrix[0][0] matrix[0][1] ... matrix[0][m-1]
...
matrix[n-1][0] ... matrix[n-1][m-1]
Output Format
Print the resulting matrix, one row per line.
Constraints
1 <= n, m <= 200
-10^9 <= matrix[i][j] <= 10^9
Example
Input
1 1
5
Output
5