-
프린터프로그래밍/알고리즘 2021. 3. 30. 16:52
풀이
from collections import deque def solution(priorities, location): answer = 0 d = deque([(v, i) for i, v in enumerate(priorities)]) # d에다가 값 인덱스 순으로 deque을 사용해 만든다. while len(d): item = d.popleft() # 0번째 값, 인덱스가 item으로 튜플형으로 넘어간다. if d and max(d)[0] > item[0]: # 만약 d의 밸류보다 item의 밸류가 크다면 뽑은 item을 d.append(item) # d의 뒤로 append함. else: # 아니라면 answer += 1 # 순서를 하나 카운트 해준다음 if item[1] == location: # 인덱스와 location이 맞다면 break # while문을 멈추고 return answer # answer를 리턴한다.
레벨 2로 넘어왔는데 막상 어려워 보이진 않지만 어려워진 느낌이다.
728x90반응형LIST'프로그래밍 > 알고리즘' 카테고리의 다른 글
체육복 (0) 2021.03.31 124 나라의 숫자 (0) 2021.03.30 두 정수 사이의 합 (0) 2021.03.28 크레인 인형뽑기 게임 (0) 2021.03.28 행렬의 덧셈 (0) 2021.03.28