Intuition
This problem asks us to find the number of magic sub-matrices. We approach this with a straight forward approach - by iterating over the entire matrix whilst maintaining a sub-grid of 3x3.
We keep track of current diagonal, anti-diagonal, sums of rows, sums of columns and all the 9 numbers currently in the sub-grid.
We need to check if all the sums are same - so we put them all in set and check if the set length is 1.
Also, we need to check if all 9 numbers in the sub-grid are unique or not. This can be done by putting them in a set too - and then check if len(set) == 9
.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since the steps to find a magic sub-grid are applied to all elements in the matrix.
-
Memory
The memory usage is since we use set inside the for loops. But if can be argued if the memory usage is constant since at a time the set and lists would contain only 3x3 = 9 elements.
— A