Problem statement

https://binarysearch.com/problems/Rotate-by-90-Degrees-Counter-Clockwise/

Solution

Equal to Leetcode 0048. Rotate Image, but rotate in different direction.

Complexity

It is O(n^2) for time and space.

Code

class Solution:
    def solve(self, M):
        n = len(M) - 1
        for i in range((n+2)//2):
            for j in range((n+1)//2):
                M[i][j], M[j][n-i], M[n-i][n-j], M[n-j][i] = M[j][n-i], M[n-i][n-j], M[n-j][i], M[i][j]
        return M