최대 1 분 소요

사용 언어: Python3

문제

스크린샷 2023-04-27 오후 3 04 14

풀이

내 풀이

from collections import deque
N, M = map(int, input().split())
lst = list(map(int, input().split()))

newLst = deque() # (환자 인덱스, 위험도)
for i, x in enumerate(lst):
    newLst.append((i, x))
# print(newLst) # deque([(0, 60), (1, 50), (2, 70), (3, 80), (4, 90)])

cnt = 0
while newLst:
    mx = max(newLst, key=lambda x:x[1]) # ✅ 위험도 기준 max
    cur = newLst.popleft()
    if cur[1] < mx[1]: # 위험도 비교 (내 위험도가 낮으면 뒤로 붙기)
        newLst.append(cur)
    else:
        cnt += 1 # 진료 받음
        if cur[0] == M: # cur[0]은 cur 환자의 인덱스
            break

print(cnt)

다른 풀이

from collections import deque
N, M = map(int, input().split())
lst = list(map(int, input().split()))
q = deque([(i, x) for i, x in enumerate(lst)]) # ✅ list comprehension으로 튜플 타입으로 큐 초기화

cnt = 0
while True:
    cur = q.popleft()
    if any(cur[1] < x[1] for x in q): # ✅ any() 사용
        q.append(cur)
    else:
        cnt += 1 # 진료 받음
        if cur[0] == M: # cur[0]은 cur 환자의 인덱스
            break

print(cnt)


💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기