-
[Stack] BOJ 17299 μ€λ±ν°μAlgorithms in Python/baekjoon 2021. 2. 3. 15:47
π λ°±μ€ 17299 μ€λ±ν°μ
β λ¬Έμ μ ν΅μ¬, νμ΄ κ³Όμ
• μ€λ±ν°μλ μμ΄ Aμμ λ±μ₯ν νμκ° F(Ai)λ³΄λ€ ν° μ μ€μμ κ°μ₯ μΌμͺ½μ μλ μλ₯Ό μλ―Ένλ€.
=> μμ΄ Aμμ λ±μ₯ν νμλ₯Ό μΉ΄μ΄νΈν΄μ μ μ₯νλ€.
=> μ€νμ μ΄μ©νμ¬ F[A[μ€νμ top]] κ°κ³Ό F[A[i]]μ λΉκ΅νλ€. F[A[i]]κ° λ ν¬λ©΄ A[i]κ° A[μ€νμ top]μ μ€λ±ν°μμ΄λ€.
β μ 체 μ½λ
import sys input = sys.stdin.readline n = int(input()) A = list(map(int, input().split())) F = [0] * 1000001 result = [-1] * n stack = [] # μ€λ±ν°μ: μμ΄ Aμμ λ±μ₯ν νμκ° F(Ai)λ³΄λ€ ν° μ μ€μμ κ°μ₯ μΌμͺ½μ μλ μλ₯Ό μλ―Έ # λ±μ₯ν νμ μΉ΄μ΄νΈ for num in A: F[num] += 1 # μ²μ μΈλ±μ€ λ£κΈ° stack.append(0) i = 1 while stack and i < n: while stack and F[A[stack[-1]]] < F[A[i]]: result[stack[-1]] = A[i] stack.pop() stack.append(i) i += 1 for x in result: print(x, end=' ')
'Algorithms in Python > baekjoon' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Dijkstra] BOJ 1261 μκ³ μ€ν (0) 2021.02.15 [Union-Find] BOJ 10775 곡ν (0) 2021.02.08 [Prefix Sum] BOJ 20002 μ¬κ³Όλ무 (0) 2021.02.03 [Sliding Window] BOJ 10025 κ²μΌλ₯Έ λ°±κ³° (0) 2021.02.03 [DFS/BFS] BOJ 18405 κ²½μμ μ μΌ (0) 2021.02.01