Problem 390511 · medium · Phase 03 Linear Management & Searching

Rotate a Square Grid Counter-Clockwise In Place

matrix · in-place · transpose

You are given an n x n grid matrix (a list of n lists of n integers). Rotate it 90 degrees counter-clockwise by modifying matrix in place. The function must return None; the tests inspect the mutated argument.

After the rotation, the top row of the original becomes the left column (read top to bottom), i.e. new[i][j] == old[j][n-1-i].

Examples

Input:  matrix = [[1, 2], [3, 4]]
Output: None   (matrix is now [[2, 4], [1, 3]])

Input:  matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: None   (matrix is now [[3, 6, 9], [2, 5, 8], [1, 4, 7]])
Explanation: the right column 3, 6, 9 becomes the top row.

Constraints

  • 1 <= n <= 300
  • Target: O(n^2) time and O(1) extra space (no second grid).

Goals

  • Rotate an n x n grid by 90 degrees counter-clockwise without allocating a second grid
  • Decompose a rotation into a transpose and a flip
  • Mutate the argument and return None
Starting Python…