[백준] 17299번 오등큰수 ★★★

거북이·2023년 7월 7일
0

백준[골드3]

목록 보기
6/21
post-thumbnail

💡문제접근

  • 이전 포스팅에 있던 [[백준] 17298번 오큰수]의 문제와 유사하여 접근하기 그나마 수월했던 문제였다.

💡코드(메모리 : 155772KB, 시간 : 1896ms)

from collections import Counter
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().strip().split()))
num_cnt = Counter(A)
stack = [0]
answer = [-1] * N

for i in range(1, N):
    while stack and num_cnt[A[stack[-1]]] < num_cnt[A[i]]:
        answer[stack.pop()] = A[i]
    stack.append(i)
print(*answer)

📌 from collections import Counter

  • 리스트 원소들의 각 빈도 수를 알고 싶을 때 Counter를 사용한다.
  • dictionary를 이용하면 각 원소의 키와 값을 대응시켜 구할 수 있지만 위와 같은 from collections import Counterimport하면 더 편리하게 코드를 작성할 수 있다.

💡소요시간 : 27m

0개의 댓글