Solution


Approach 1: Copy Directly

Intuition and Algorithm

The transpose of a matrix A with dimensions R x C is a matrix ans with dimensions C x R for which ans[c][r] = A[r][c].

Let's initialize a new matrix ans representing the answer. Then, we'll copy each entry of the matrix as appropriate.

Complexity Analysis

  • Time Complexity: , where and are the number of rows and columns in the given matrix A.

  • Space Complexity: , the space used by the answer.


Analysis written by: @awice.