๋ฌธ์
https://school.programmers.co.kr/learn/courses/30/lessons/118667
ํ์ด
# ํ๋ก๊ทธ๋๋จธ์ค 2๋จ๊ณ - ๋ ํ ํฉ ๊ฐ๊ฒ ๋ง๋ค๊ธฐ
from collections import deque
def solution(queue1,queue2):
answer = 0
queue1 = deque((queue1))
queue2 = deque((queue2))
sum_q1 = sum(queue1)
sum_q2 = sum(queue2)
limit = len(queue1) * 3
if (sum_q1 + sum_q2) % 2 != 0:
return -1
while True:
if sum_q1 > sum_q2:
v = queue1.popleft()
queue2.append(v)
sum_q1 -= v
sum_q2 += v
answer += 1
elif sum_q1 < sum_q2:
v = queue2.popleft()
queue1.append(v)
sum_q1 += v
sum_q2 -= v
answer += 1
else:
break
if answer == limit:
answer = -1
break
return answer
print(solution([1, 2, 1, 2],[1, 10, 1, 2]))
deque๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ๊ฒ ํ ์ ์๋ ๋ฌธ์ ์์ง๋ง ์๊ฐ์ด๊ณผ๊ฐ ๋์์ limit์ด๋ผ๋ ์ ํ์ ๋์๋ค.