You are given an m x n integer matrix where m and n are both multiples of 4. The matrix is a grid of non-overlapping 4x4 blocks. Each block contains 15 distinct numbers from 1 to 16, and the one number missing from the block is replaced by 0.
Find the missing value of every block, sort the blocks in ascending order of their missing values, and place the sorted blocks back into the grid in row-major order (left to right along a row of blocks, then top to bottom). Return the reassembled matrix.
Function Signature
def sort_matrix_blocks(matrix: list[list[int]]) -> list[list[int]]:
...
Rules
-
The block in block row
br
and block column
bc
covers rows
4*br
to
4*br + 3
and columns
4*bc
to
4*bc + 3
of the matrix.
-
Block positions are numbered in row-major order. With
n // 4
blocks per row, position
k
is block row
k // (n // 4)
and block column
k % (n // 4)
.
-
The block with the smallest missing value goes to position 0 (the top-left), the block with the next smallest goes to position 1, and so on.
-
A block moves as a unit and its 16 cells keep their layout. The
0
stays where it is inside its block and is not replaced by the missing value.
-
Return a matrix with the same dimensions
m x n
. Because all missing values are distinct, the result is unique.
Constraints
-
4 <= m, n <= 40
-
m
and
n
are multiples of 4
-
Each 4x4 block contains exactly 15 distinct numbers from 1 to 16 and one
0
-
All missing values across blocks are distinct
-
Since each missing value lies in 1 to 16 and no two are equal, a valid input has at most 16 blocks:
(m // 4) * (n // 4) <= 16
Examples
Example 1:
Input: matrix = [
[1, 2, 3, 4, 16, 15, 14, 13],
[5, 6, 7, 8, 12, 11, 10, 9],
[9, 10, 11, 12, 8, 7, 6, 5],
[13, 14, 0, 16, 4, 3, 0, 1]
]
Output: [
[16, 15, 14, 13, 1, 2, 3, 4],
[12, 11, 10, 9, 5, 6, 7, 8],
[8, 7, 6, 5, 9, 10, 11, 12],
[4, 3, 0, 1, 13, 14, 0, 16]
]
The left block contains every number from 1 to 16 except 15, and the right block is missing 2. The right block has the smaller missing value, so it takes the left position and the original left block moves to the right.
Example 2:
Input: matrix = [
[0, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
Output: [
[0, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
The only block is missing 1. With one block there is nothing to reorder, so the output equals the input.
Example 3:
Input: matrix = [
[1, 2, 3, 4, 16, 15, 14, 13],
[5, 6, 7, 8, 12, 11, 10, 9],
[0, 10, 11, 12, 8, 7, 6, 5],
[13, 14, 15, 16, 4, 0, 2, 1],
[1, 5, 9, 13, 0, 2, 3, 4],
[2, 6, 10, 14, 5, 6, 7, 8],
[3, 7, 11, 15, 9, 10, 11, 12],
[4, 8, 0, 16, 13, 14, 15, 16]
]
Output: [
[0, 2, 3, 4, 16, 15, 14, 13],
[5, 6, 7, 8, 12, 11, 10, 9],
[9, 10, 11, 12, 8, 7, 6, 5],
[13, 14, 15, 16, 4, 0, 2, 1],
[1, 2, 3, 4, 1, 5, 9, 13],
[5, 6, 7, 8, 2, 6, 10, 14],
[0, 10, 11, 12, 3, 7, 11, 15],
[13, 14, 15, 16, 4, 8, 0, 16]
]
In row-major order the four blocks are missing 9 (top-left), 3 (top-right), 12 (bottom-left) and 1 (bottom-right). The ascending order is 1, 3, 9, 12, so the old bottom-right block moves to the top-left, the top-right block stays where it is, the old top-left block moves to the bottom-left, and the old bottom-left block moves to the bottom-right.