|
์ฝ๋ ์์ฑํ๊ธฐ
from collections import deque
def solution(priorities, location):
answer = 0
deq = deque([(v, i) for i, v in enumerate(priorities)])
# print(deq)
while deq:
item = deq.popleft()
if deq and item[0] < max(deq)[0]:
deq.append(item)
else:
answer += 1
if item[1] == location:
break
return answer
priorities1 = [2, 1, 3, 2]
priorities2 = [1, 1, 9, 1, 1, 1]
# print(solution(priorities1, 2))
print(solution(priorities2, 0))
Python
๋ณต์ฌ
|
์ฝ๋ ์ค๋ช
ํ๊ธฐ
1.
์ ์
์ ์ถ โ> deque ์ฌ์ฉ
2.
index๋ ์์, value๊ฐ์ ์ค์๋
3.
๊ฐ์ ์ถ์ถํ ํ์
a.
์ค์๋๊ฐ โ๊ฐ์ฅ๋์์ค์๋โ๊ฐ ์๋๋ผ๋ฉด ๋ค์ deq์ ๋ฃ๋๋ค.
b.
์ค์๋๊ฐ โ๊ฐ์ฅ๋์์ค์๋โ๋ผ๋ฉด ๋ค์๋ฃ์ง์๊ณ answer๋ฒ์งธ๋ก ๋ฝํ๊ฒ์ ๋ํ๋ธ๋ค.
c.
์ถ์ถํ์ ๋, ๊ฐ์ฅ ๋์ ์ค์๋์ด๋ฉด์, location๊ณผ ๊ฐ๋ค๋ฉด ๊ทธ๊ฒ์ด ์ ๋ต์ด๋๊น break