문제풀이/백준(Boj) 문제풀이

[백준][큐 & 덱] 1966. 프린터 큐 (파이썬/Python)

얄루몬 2021. 12. 6. 16:47

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

t = int(input())

for _ in range(t):
    n, m = map(int,input().split())
    q = deque(list(map(int,input().split())))
    cnt = 0

    while q:
        maxQ = max(q)
        front = q.popleft()
        m -= 1

        if maxQ == front:
            cnt += 1
            if m < 0 :
                print(cnt)
                break
        else:
            q.append(front)
            if m < 0 :
                m = len(q) - 1

# m이 0보다 작아지는 경우는 그 위치만큼 q를 돌려서 확인했다는 것이기 때문에 m < 0 일때 cnt 값을 출력해주면 그것이 답이 된다.