diff --git a/misc/spiral_matrix.py b/misc/spiral_matrix.py new file mode 100644 index 0000000..cb5d10f --- /dev/null +++ b/misc/spiral_matrix.py @@ -0,0 +1,26 @@ +# Complexity Analysis + +# Time Complexity: O(N)O(N), where NN is the total number of elements in the input matrix. We add every element in the matrix to our final answer. + +# Space Complexity: O(N)O(N), the information stored in seen and in ans. + + +class Solution(object): + def spiralOrder(self, matrix): + if not matrix: return [] + R, C = len(matrix), len(matrix[0]) + seen = [[False] * C for _ in matrix] + ans = [] + dr = [0, 1, 0, -1] + dc = [1, 0, -1, 0] + r = c = di = 0 + for _ in range(R * C): + ans.append(matrix[r][c]) + seen[r][c] = True + cr, cc = r + dr[di], c + dc[di] + if 0 <= cr < R and 0 <= cc < C and not seen[cr][cc]: + r, c = cr, cc + else: + di = (di + 1) % 4 + r, c = r + dr[di], c + dc[di] + return ans