You’re on an AR (augmented reality) team at a large e-commerce marketplace with 20M+ daily active users. Product pages render interactive 3D previews, and a performance regression has been traced to a hot path that rotates square “texture tiles” represented as integer matrices. Allocating a second matrix for every rotation spikes memory and causes frame drops on mid-range devices.
Your task is to rotate these tiles in-place.
Given an n × n matrix matrix of integers, rotate it 90 degrees clockwise in-place.
matrix: list[list[int]] where matrix[i][j] is the value at row i, column j.matrix directly.A 90° clockwise rotation means the element at position (r, c) moves to (c, n - 1 - r).
Input:
matrix = [[1,2,3],[4,5,6],[7,8,9]]
After rotation (in-place):
[[7,4,1],[8,5,2],[9,6,3]]
Explanation (key moves):
1 at (0,0) moves to (0,2)7 at (2,0) moves to (0,0)Input:
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
After rotation (in-place):
[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
n × n).n = 1 should leave the matrix unchanged.n × n matrix.