백준 16539번: Chain #Python

ColorlessDia·2025년 4월 28일

algorithm/baekjoon

목록 보기
526/807
from collections import deque

N = int(input())
A_stack = deque(map(int, input().split()))

chain_list = [0] * N
stack = deque()
stack.append(A_stack.pop())

for i in range(N - 2, -1, -1):
    A = A_stack.pop()
    
    if A < stack[-1]:
        chain_list[i] = len(stack)
        stack.append(A)
        continue

    while 0 < len(stack):
        stack.pop()

        if len(stack) == 0:
            stack.append(A)
            break

        if A < stack[-1]:
            chain_list[i] = len(stack)
            stack.append(A)
            break

print(*chain_list)

0개의 댓글