Problem overview
The method transpose receives matrix, an Array<IntArray> with m rows and n columns, and returns a new Array<IntArray> with n rows and m columns. The value in row i, column j of the input lands in row j, column i of the output.
For [[6, 0, -3], [2, 9, 4]] (2 rows of 3) the answer has 3 rows of 2. The top row becomes the left column and the bottom row becomes the right column:
6 2
0 9
-3 4
A single column turns into a single row: [[7], [-1], [5], [10]] gives [[7, -1, 5, 10]]. The tests print each row as values separated by spaces.
Approach
There is no clever trick here. Every value has exactly one destination, and the rule for finding it is the swap of its two indexes. So the plan is to create the result grid with the swapped shape, visit every cell of the input once, and copy it to result[j][i]. That touches m * n cells, which is the O(m * n) the statement asks for, and it cannot be beaten, because every value has to be read.
The new grid is needed because the shape changes. A 2 by 3 grid cannot be turned into a 3 by 2 grid inside the same arrays. (Only square grids can be transposed in place, by swapping pairs across the diagonal, but this lesson returns a new grid.)
Step by step
- Measure the input.
mismatrix.size, the number of rows.nismatrix[0].size, the number of columns. Every row has the same length, so the first row is enough, and the constraints guarantee at least one row. - Build the result.
Array(n) { IntArray(m) }createsnrows, and the block runs once per row to make anIntArrayofmzeros. Because the block runs for each row, the rows are separate arrays, not one array sharedntimes. - Loop over the result's rows. The outer loop variable
jgoes over0 until n. Rowjof the result will hold columnjof the input. - Walk down that column. The inner loop variable
igoes over0 until m, from the top row to the bottom row of the input. - Copy with the indexes traded.
result[j][i] = matrix[i][j]. - Return
result.
The solution in Kotlin
class Solution {
fun transpose(matrix: Array<IntArray>): Array<IntArray> {
val m = matrix.size
val n = matrix[0].size
val result = Array(n) { IntArray(m) }
for (j in 0 until n) {
for (i in 0 until m) {
result[j][i] = matrix[i][j]
}
}
return result
}
}
Kotlin details worth knowing:
Array<IntArray>is an array of primitiveintarrays. It is the usual choice for a grid of integers, andmatrix[i][j]reads and writes it directly.0 until nstops beforen. Writing0..nwould includenand fail with an out-of-bounds exception on the last pass.- A new
IntArray(m)is filled with zeros, but every cell gets overwritten here, so the zeros never show.
Complexity
Time: the two loops run n * m times and each pass does a constant amount of work: O(m * n). Space: the result holds m * n values, so the extra space is also O(m * n). That space is the answer itself, which the method has to return.
Edge cases and common mistakes
- Making the result
mbyn. For a grid that isn't square,Array(m) { IntArray(n) }is the wrong shape, andresult[j][i]then fails with anArrayIndexOutOfBoundsException. The result hasnrows ofmvalues. - Forgetting to swap the indexes.
result[i][j] = matrix[i][j]just copies the grid. - One row or one column. Both work with no special case. A 4 by 1 input gives a 1 by 4 result,.
- Square grids.
[[4, -7], [2, 0]]gives[[4, 2], [-7, 0]]. The cells on the main diagonal stay where they are, and the others trade places across it.
Testing
| Input | Output |
|---|---|
[[6, 0, -3], [2, 9, 4]] | [[6, 2], [0, 9], [-3, 4]] |
[[7], [-1], [5], [10]] | [[7, -1, 5, 10]] |
[[4, -7], [2, 0]] | [[4, 2], [-7, 0]] |
