πŸ—οΈ Algorithm/🟩 λ°±μ€€

🟩 [λ°±μ€€] [Python] [Gold5] 2493번_탑

Dbswnstjd 2023. 3. 13. 21:43

문제

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

 

2493번: 탑

첫째 쀄에 νƒ‘μ˜ 수λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ •μˆ˜ N이 주어진닀. N은 1 이상 500,000 μ΄ν•˜μ΄λ‹€. λ‘˜μ§Έ μ€„μ—λŠ” N개의 νƒ‘λ“€μ˜ 높이가 직선상에 놓인 μˆœμ„œλŒ€λ‘œ ν•˜λ‚˜μ˜ λΉˆμΉΈμ„ 사이에 두고 주어진닀. νƒ‘λ“€μ˜ λ†’μ΄λŠ” 1

www.acmicpc.net

풀이

# λ°±μ€€ 2493번 문제 - 탑
import sys
input = sys.stdin.readline
n = int(input())
tops = list(map(int, input().split()))
answer = [0] * n
stack = []
for i in range(len(tops)):
    while stack:
        if tops[stack[-1][0]] < tops[i]:
            stack.pop()
        else:
            answer[i] = stack[-1][0] + 1
            break
    stack.append((i, tops[i]))
print(*answer)