25(June) Left Rotate Matrix K times
25. Left Rotate Matrix K times
The problem can be found at the following link: Question Link
Problem Description
You are given an integer k
and a matrix mat
. Return a matrix where it is rotated left k
times.
Example:
Input:
k = 1
mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Output:
2 3 1
5 6 4
8 9 7
Explanation: After rotating the matrix by one position to the left, it becomes:
2 3 1
5 6 4
8 9 7
My Approach
Initialization:
Determine the number of rows (
n
) and columns (m
) in the matrixmat
.Create a new matrix
ans
of the same dimensions to store the result after rotation.
Rotation Calculation:
Calculate the effective number of rotations needed using
k % m
to handle cases wherek
is larger thanm
.Iterate over each element in the original matrix
mat
, and determine its new position in the matrixans
after rotation.
Return:
Return the matrix
ans
containing the elements afterk
left rotations.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(n * m), as we iterate over all elements in the matrix once.
Expected Auxiliary Space Complexity: O(n * m), as we use an additional matrix of the same size to store the rotated elements.
Code (C++)
class Solution {
public:
vector<vector<int>> rotateMatrix(int k, vector<vector<int>> mat) {
int n = mat.size();
int m = mat[0].size();
vector<vector<int>> ans(n, vector<int>(m));
k %= m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int newCol = (j - k + m) % m;
ans[i][newCol] = mat[i][j];
}
}
return ans;
}
};
Code (Java)
class Solution {
int[][] rotateMatrix(int k, int mat[][]) {
int n = mat.length;
int m = mat[0].length;
int[][] ans = new int[n][m];
k %= m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int newCol = (j - k + m) % m;
ans[i][newCol] = mat[i][j];
}
}
return ans;
}
}
Code (Python)
class Solution:
def rotateMatrix(self, k, mat):
n = len(mat)
m = len(mat[0])
ans = [[0] * m for _ in range(n)]
k %= m
for i in range(n):
for j in range(m):
new_col = (j - k + m) % m
ans[i][new_col] = mat[i][j]
return ans
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated