priorities | location | return |
---|---|---|
[2, 1, 3, 2] | 2 | 1 |
[1, 1, 9, 1, 1, 1] | 0 | 5 |
프린터 순서를 알맞게 정렬한 다음에 0인덱스부터 location까지를 세려고 했는데 이렇게 하면 시간이 오래걸려 실패한다.
따라서 프린터 순서를 정렬하는 것에 초점을 두는게 아니고, location 즉 알아야하는 원소가 0번째에 오기까지 pop을 하고 카운트를 세는 방법을 써야한다.
- 중요도가 같을 경우를 판별하기 인덱스를 함께 저장한다.
- 첫번째 원소를 pop하여 printer에 있는 나머지 원소의 중요도와 비교하여 더 작은게 있다면 printer의 제일 뒤에 append하고 break한다.
- 만약 없다면 그 원소의 인덱스와 location을 비교한다.
location과 같다면 카운트를 리턴한다.
def solution(priorities, location):
printer = []
for i, priority in enumerate(priorities):
printer.append((priority, i))
cnt = 0
while True:
top = printer.pop(0)
flag = 0
for p in printer:
if top[0] < p[0]:
flag += 1
printer.append(top)
break
if flag == 0:
cnt += 1
if top[1] == location:
return cnt