LeetCode 48. Rotate Image

November 5, 2024

Working through more matrix problems today.

Solution

I used a method where we transpose the matrix, then reverse the rows. This is memory efficient in space and locality.

1class Solution:
2 def rotate(self, matrix: List[List[int]]) -> None:
3 """
4 Do not return anything, modify matrix in-place instead.
5 """
6 for i in range(len(matrix)):
7 for j in range(i+1, len(matrix)):
8 matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
9
10 for i in range(len(matrix)):
11 matrix[i] = matrix[i][::-1]
12
13 return matrix

(n) = n^2 since we have nested loops dependent on input size.

LeetCode 36. Valid Sudoku

LeetCode 289. Game of Life