[PY] 10773 : 제로

https://www.acmicpc.net/problem/10773

장황한 이야기에 비해 알아야 할 것이 별로 없다.
0이라고 나오는 순간 방금 숫자에 대한 내용을 지우면 되니, 0은 곧 pop을 하라는 신호와 같다.

import sys

def empty(idx):
    if myStack[idx] == 0:
        return True
    else:
        return False

def pop(idx):
    if not empty(idx):
        output = myStack[idx]
        myStack[idx] = 0
        return output
    else:
        return -1
    
def push(item, idx):
    myStack[idx] = item
    return 




commandList = int(sys.stdin.readline())
myStack = [0] * 100000
curStackIdx = -1

for i in range(commandList):
    command = int(sys.stdin.readline().strip())

    if command == 0:
        # 여긴 Push N 전용
        popResult = pop(curStackIdx)
        if popResult != -1:
            curStackIdx -= 1
        
    else:
        curStackIdx += 1
        push(command, curStackIdx)
        

total = 0
for i in range(100000):
    total += myStack[i]

print(total)

'문제풀이' 카테고리의 다른 글

[PY] 17608 : 막대기  (0) 2025.03.24
[PY] 9012 : 괄호  (0) 2025.03.24
[PY] 10828 : 스택  (0) 2025.03.24
[PY] 2110 : 공유기 설치  (0) 2025.03.24
[PY] 11503 : 가장 긴 증가하는 부분 수열  (0) 2025.03.23