[백준] 2493번 탑 - 파이썬/스택

JinUk Lee·2023년 5월 16일
0

백준 알고리즘

목록 보기
56/74

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



N = int(input())

N_list = list(map(int,input().split()))

stack = []

ans = []

for i in range(N):
    while stack:

        if stack[-1][1] > N_list[i]:
            ans.append(stack[-1][0]+1)
            break
        else:
            stack.pop()

    if not stack:
        ans.append(0)
    stack.append([i,N_list[i]])

print(' '.join(map(str,ans)))

스택에서 자주 출제되는 유형의 문제인데 좋은 테크닉을 배웠다.

참고 블로그

profile
개발자 지망생

0개의 댓글