You are given an m x n integer matrix matrix. Perform the following three transformations in order:
-
Rotate the matrix 90 degrees clockwise.
-
For each row in the rotated matrix, replace every element with the running sum (prefix sum) of that row.
-
Perform a spiral traversal of the resulting matrix in clockwise order, starting from the top-left corner.
Return the final 1D array after all three transformations.
Function Signature
def matrix_trio(matrix: list[list[int]]) -> list[int]:
Rules
-
Rotating an
m x n
matrix clockwise produces an
n x m
matrix. The element at row
i
, column
j
of the input ends up at row
j
, column
m - 1 - i
.
-
The running sum goes left to right within each row of the rotated matrix, so element
j
of a row becomes the sum of elements
0
through
j
of that row.
-
The spiral reads the top row left to right, then the right column top to bottom, then the bottom row right to left, then the left column bottom to top, and repeats on the inner rectangle until every element has been read once. The output has exactly
m * n
values.
Constraints
-
m == len(matrix)
-
n == len(matrix[i])
-
1 <= m, n <= 50
-
-1000 <= matrix[i][j] <= 1000
Examples
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [7,11,12,15,18,15,9,8,13]
Rotating clockwise gives [[7,4,1],[8,5,2],[9,6,3]], and the row running sums give [[7,11,12],[8,13,15],[9,15,18]]. The spiral reads the outer ring 7, 11, 12, 15, 18, 15, 9, 8 and then the center 13.
Input: matrix = [[1,2],[3,4],[5,6]]
Output: [5,8,9,12,10,6]
The 3 x 2 input rotates into the 2 x 3 matrix [[5,3,1],[6,4,2]], and the running sums give [[5,8,9],[6,10,12]]. The spiral reads the top row 5, 8, 9, then 12 down the right column, then 10, 6 along the bottom row.
Input: matrix = [[2],[-5],[4]]
Output: [4,-1,1]
A single column rotates into the single row [[4,-5,2]], which is the column read from bottom to top. Its running sums are 4, -1, 1, and the spiral of a one-row matrix is that row.