2025-02-21 17:50:51

스택문제다

 

스택에 인덱스번호랑 값이랑 넣어놓자

 

import sys
n=int(sys.stdin.readline().rstrip())
numbers=list(map(int,sys.stdin.readline().rstrip().split()))
stack=[]
result=[0]*n
for i in range(n-1,-1,-1):
    while stack and numbers[i]>stack[-1][0]:
        value,index=stack.pop()
        result[index]=i+1

    stack.append([numbers[i],i])
print(*result)

역순으로 해야 생각하기 편하다.

'IT > ps' 카테고리의 다른 글

백준 15486번 퇴사2  (0) 2025.02.25
백준 1038번 감소하는 수  (0) 2025.02.21
백준 2538번 영역 구하기  (0) 2025.02.20
백준 15683번 감시  (0) 2025.02.19
백준 4179번 불!  (0) 2025.02.19