SDE & MLE Prep

867. Transpose Matrix

Problem

Return the transpose of matrix: flip it over its main diagonal, turning rows into columns.

Example: [[1,2,3],[4,5,6]] → [[1,4],[2,5],[3,6]]

Element (i, j) moves to (j, i), so an R × C matrix becomes C × R (a new matrix unless it's square).

Complexity: O(R·C).

class Solution:
    def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
        return [list(col) for col in zip(*matrix)]