본문 바로가기
🧑‍💻 Coding Test/Stack Queue

[LeetCode] 1441. Build an Array with Stack Operations

by 헤이즐넛 좋아하는 개발자 2025. 1. 26.

I. Description


II. Code

class Solution:
    def buildArray(self, target: List[int], n: int) -> List[str]:
        # 초기화
        output_list = []
        checked = 1
        
        for i in range(1, n+1):
            if i in target:
                while checked != i:
                    output_list.append("Push")
                    output_list.append("Pop")
                    checked += 1
                output_list.append("Push")
                checked += 1
                
        return output_list

과정이 간단하게 이루어진다.