The problem asks to find the sum of both diagonals in a square matrix. This is fairly straightforward. Now, if you are a programmer who knows concepts such as DFS, BFS you would realize that you don’t need to traverse the matrix in a manner, you can just get the diagonals.
After that there just arises one edge case, if the length of either row
or col
is odd, the middle value would be calculated twice. In that case, just subtract the middle value once from the final answer.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is as since we would be visiting all characters in the string at least once.
-
Memory
The memory usage is since we are using a set to store visited points.
— A