프린터 큐 (1966)

Lee Hyun Joon ·2023년 4월 8일
0

알고리즘정리

목록 보기
15/17

문제 설명

문제에서 요구하는 코드는 queue에서 가중치를 고려해서 pop을 적용하는 방법을 고안해야 한다.

이 문제를 어떻게 고민하다가 그냥 현재 queue 내의 제일 큰 가중치를 파악해서 pop하고자 하는 타깃의 index번호가 갖고 있는 가중치 값과 비교해서 pop 혹은 reinsert하는 방법을 택했다.

import sys
import collections

total = int(input())
pq = collections.deque([])

for i in range(total):
    current_total, target = map(int, sys.stdin.readline().split())
    current_weight = list(map(int, sys.stdin.readline().split()))
    max_weights = list(set(sorted(current_weight))) #현재 queue 내에 있는 가중치 리스트에 대한 정보
    count = collections.Counter(current_weight) #Count 정보도 포함
    
    for j in range(current_total):
        pq.append([j,current_weight[j]])
        
    cnt = 0 # 몇 번째인지 출력하는 변수
    while pq:
        if pq[0][1] < max_weights[-1]:
           pq.append(pq.popleft()) # reinsert 
        elif  pq[0][1] == max_weights[-1]:
            cnt+=1
            # print(cnt)
            if pq[0][0] == target:
                print(cnt)
                break 
            else:
                count[max_weights[-1]] -=1 
                if count[max_weights[-1]] == 0:
                    max_weights.pop() # pop and get the another max value
                pq.popleft()
    pq.clear()
                
profile
우당탕탕 개발 지망생

0개의 댓글