Write spiral_order(grid: list[list[int]]) -> list[int] that returns every value of an m x n grid in clockwise spiral order. Read the top row left to right, then down the right column, then the bottom row right to left, then up the left column. Then do the same one layer further in, until every cell has been read exactly once.
spiral_order([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]) # [1, 2, 3, 6, 9, 8, 7, 4, 5]
spiral_order([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]]) # [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
spiral_order([[1], [2], [3]]) # [1, 2, 3]
spiral_order([[7]]) # [7]
1 <= m, n <= 400; every row has the same length; values are in[-1000, 1000].- The grid is not always square. Don't change
grid. - Aim for O(m·n) time: read each cell once.
Show hint
keep four boundaries, top, bottom, left and right. After you read a side, move that boundary one step inwards. Before reading the bottom row and the left column, check that top <= bottom and left <= right still hold, or a last single row or column gets read twice.