"""
    https://leetcode.com/problems/spiral-matrix/
    54. Spiral Matrix (Medium)

    [풀이방법]
    first row -> last col -> last row -> first col 순으로 순회하며 결과저장
    한 라인이 돌고나면 matrix의 폭을 조정
    모든 element를 조회할때까지 while이 돌다가 종료
"""

class Solution(object):
    def spiralOrder(self, matrix):
        answer = []
        x = [0, len(matrix)-1]
        y = [0, len(matrix[0])-1]
        while x[0] <= x[1] or y[0] <= y[1]:
            for i in range(y[0], y[1]+1):
                answer.append(matrix[x[0]][i])
            x[0] += 1
            if x[0] > x[1] or y[0] > y[1]:
                break

            for i in range(x[0], x[1]+1):
                answer.append(matrix[i][y[1]])
            y[1] -= 1
            if x[0] > x[1] or y[0] > y[1]:
                break

            for i in range(y[1], y[0]-1, -1):
                answer.append(matrix[x[1]][i])
            x[1] -= 1
            if x[0] > x[1] or y[0] > y[1]:
                break

            for i in range(x[1], x[0]-1, -1):
                answer.append(matrix[i][y[0]])
            y[0] += 1
            if x[0] > x[1] or y[0] > y[1]:
                break
        
        return answer

sol = Solution()
print(sol.spiralOrder([[1,2,3],[4,5,6],[7,8,9]]))
print(sol.spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]]))

"""
    https://leetcode.com/problems/spiral-matrix/
    54. Spiral Matrix (Medium)

    [풀이방법]
    first row -> last col -> last row -> first col 순으로 순회하며 결과저장
    한 라인이 돌고나면 matrix의 폭을 조정
    모든 element를 조회할때까지 while이 돌다가 종료
"""

class Solution(object):
    def spiralOrder(self, matrix):
        answer = []
        x = [0, len(matrix)-1]
        y = [0, len(matrix[0])-1]
        while x[0] <= x[1] or y[0] <= y[1]:
            for i in range(y[0], y[1]+1):
                answer.append(matrix[x[0]][i])
            x[0] += 1
            if x[0] > x[1] or y[0] > y[1]:
                break

            for i in range(x[0], x[1]+1):
                answer.append(matrix[i][y[1]])
            y[1] -= 1
            if x[0] > x[1] or y[0] > y[1]:
                break

            for i in range(y[1], y[0]-1, -1):
                answer.append(matrix[x[1]][i])
            x[1] -= 1
            if x[0] > x[1] or y[0] > y[1]:
                break

            for i in range(x[1], x[0]-1, -1):
                answer.append(matrix[i][y[0]])
            y[0] += 1
            if x[0] > x[1] or y[0] > y[1]:
                break
        
        return answer

sol = Solution()
print(sol.spiralOrder([[1,2,3],[4,5,6],[7,8,9]]))
print(sol.spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]]))

https://leetcode.com/problems/spiral-matrix/     54. Spiral Matrix (Medium)      [풀이방법]     first row -> last col -> last row -> first col 순으로 순회하며 결과저장     한 라인이 돌고나면 matrix의 폭을 조정     모든 element를 조회할때까지 while이 돌다가 종료