- the initial matrix is of size (n*m)
- create a new matrix with the inverse dimensions of (m*n)
- iterate through the matrix, reversing the location of the element in the new matrix
- return the new matrix
π€What Did I Struggle With?
~
π‘ Explanation of Solution
same as initial thoughts
β Complexity Analysis
Time Complexity: O(n*m) as we are iterating over every element in the matrix
Space Compleixty: O(m*n) for the construction of the transposed matrix
π» Implementation of Solution
class Solution {public: vector<vector<int>> transpose(vector<vector<int>>& matrix) { int n = matrix.size(); // Number of rows in the input matrix int m = matrix[0].size(); // Number of columns in the input matrix vector<vector<int>> newMatrix(m, vector<int>(n)); // Transposed dimensions for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { newMatrix[j][i] = matrix[i][j]; // Transpose logic } } return newMatrix; }};