Transpose Matrix
Real-world version: Flip a Reading Grid Onto Its SideThe statement here is our own: the same inputs, outputs and technique.
A logging rig writes its readings into matrix, a rectangle with one row per sensor and one column per sample instant.
The analysis stage wants the same readings laid out the other way round: one row per sample instant and one column per sensor. Mirror the rectangle across the line running from its top-left corner down and to the right, so the reading stored at row r, column c ends up at row c, column r.
Return the mirrored rectangle.
Examples
Example 1
- Input
- matrix = [[9, -4, 6], [2, 8, -1]]
- Output
- [[9, 2], [-4, 8], [6, -1]]
The rig had two sensors and three sample instants, so the answer has three rows of two readings. The reading 6 sat at row 0, column 2 and now sits at row 2, column 0.
Example 2
- Input
- matrix = [[4], [5], [6]]
- Output
- [[4, 5, 6]]
Three sensors sampled once each. Mirroring turns the single column into a single row.
Example 3
- Input
- matrix = [[2, 6], [8, 3]]
- Output
- [[2, 8], [6, 3]]
The two readings on the mirror line, 2 and 3, stay where they are, and 6 and 8 trade places.
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 1000
- 1 <= m * n <= 10^5
- -10^9 <= matrix[i][j] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def transpose(matrix: list[list[int]]) -> list[list[int]]:public int[][] transpose(int[][] matrix)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 8 test cases: 8 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Reshape the MatrixReal-world version: Relaying the Contact SheetEasy
- Shift 2D GridReal-world version: Rolling the Tile Board ForwardEasy
- Find Winner on a Tic Tac Toe GameReal-world version: Who Comes Out Ahead on the Three-by-ThreeEasy
- Convert 1D Array Into 2D ArrayReal-world version: Laying the Tiles Into a RackEasy