You are given an n x n
2D matrix
representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Constraints:
n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to rotate an image involves creating a completely new image and copying the values from the original image to the correct rotated positions in the new image. This approach directly moves each value without trying to optimize the process, checking every single position.
Here's how the algorithm would work step-by-step:
def rotate_image_brute_force(image):
matrix_size = len(image)
# Create a new matrix to store the rotated image
rotated_image = [[0] * matrix_size for _ in range(matrix_size)]
for row in range(matrix_size):
for column in range(matrix_size):
# Calculate the new row and column after rotation
new_row = column
new_column = matrix_size - 1 - row
# Place the element in the correct position in the rotated image
rotated_image[new_row][new_column] = image[row][column]
# Replace the original image with the rotated image
for row in range(matrix_size):
for column in range(matrix_size):
image[row][column] = rotated_image[row][column]
Rotating an image efficiently involves two key transformations done in place. First, we flip the image along its main diagonal. Then, we flip the image horizontally to achieve the desired 90-degree clockwise rotation.
Here's how the algorithm would work step-by-step:
def rotate_image(matrix):
matrix_size = len(matrix)
# Transpose the matrix (flip over main diagonal)
for i in range(matrix_size):
for j in range(i, matrix_size):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Reflect each row horizontally
# This completes the 90-degree clockwise rotation
for row in matrix:
row.reverse()
#Horizontal flip is necessary for clockwise rotation.
return matrix
Case | How to Handle |
---|---|
Null or empty matrix | Check for null or empty matrix and return immediately if true to avoid NullPointerException or errors. |
Non-square matrix | Throw an IllegalArgumentException, or return false/error code, because the rotation is undefined for non-square matrices. |
1x1 matrix | The rotation does nothing, so return immediately as it's already rotated. |
2x2 matrix | The standard in-place rotation algorithm should correctly handle this minimum size efficiently. |
Matrix with Integer.MAX_VALUE or Integer.MIN_VALUE | The in-place rotation does not perform arithmetic operations that can overflow; it only involves swapping values so it's safe. |
Large matrix (e.g., 1000x1000) exceeding memory constraints | The in-place rotation algorithm operates within the original matrix, minimizing memory usage and avoiding excessive memory allocation. |
Matrix contains all identical values | The algorithm should still correctly rotate the image regardless of the element values within the matrix. |
Matrix contains negative numbers, zeros, and positive numbers | The in-place rotation operates on indices and swapping, so the algorithm will function correctly regardless of the matrix's number distribution. |