diff --git a/Arrays/Spiral_Matrix.py b/Arrays/Spiral_Matrix.py new file mode 100644 index 0000000..b0798fd --- /dev/null +++ b/Arrays/Spiral_Matrix.py @@ -0,0 +1,39 @@ +''' + Given a 2D array, print it in spiral form. See the following examples. + +Input: + 1 2 3 4 + 5 6 7 8 + 9 10 11 12 + 13 14 15 16 +Output: +1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 + +''' + +# 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