728x90
๋ฐ์ํ
- ๋ฌด์ง์ฑ ์ฝ๋ฉ...
def solution(queue1, queue2):
answer = 0
lenQ = len(queue1) * len(queue2)
while answer < lenQ:
if sum(queue1) > sum(queue2):
queue2.append(queue1.pop(0))
else:
queue1.append(queue2.pop(0))
answer += 1
if sum(queue1) == sum(queue2):
return answer
return -1
- ํ ์คํธ ์ผ์ด์ค๋ ์ ๋์ง๋ง,
ํ
์คํธ 1
์
๋ ฅ๊ฐ ใ [3, 2, 7, 2], [4, 6, 5, 1]
๊ธฐ๋๊ฐ ใ 2
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
ํ
์คํธ 2
์
๋ ฅ๊ฐ ใ [1, 2, 1, 2], [1, 10, 1, 2]
๊ธฐ๋๊ฐ ใ 7
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
ํ
์คํธ 3
์
๋ ฅ๊ฐ ใ [1, 1], [1, 5]
๊ธฐ๋๊ฐ ใ -1
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
- ์ ์ถ ํ ์ฑ์ ํ๋ฉด ํ๋ฆฐ๋ค.
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 2 ใ ์คํจ (0.00ms, 10.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.00ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.18ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.22ms, 10.1MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.49ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.35ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (2.85ms, 10.1MB)
ํ
์คํธ 10 ใ ํต๊ณผ (18.68ms, 10.2MB)
ํ
์คํธ 11 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 12 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 13 ใ ํต๊ณผ (470.15ms, 11.1MB)
ํ
์คํธ 14 ใ ํต๊ณผ (581.42ms, 11.4MB)
ํ
์คํธ 15 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 16 ใ ํต๊ณผ (3141.01ms, 17.5MB)
ํ
์คํธ 17 ใ ํต๊ณผ (466.77ms, 16.6MB)
ํ
์คํธ 18 ใ ํต๊ณผ (5542.90ms, 29.9MB)
ํ
์คํธ 19 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 20 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 21 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 22 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 23 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 24 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 25 ใ ํต๊ณผ (0.45ms, 10.2MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.21ms, 10.1MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.39ms, 10.2MB)
ํ
์คํธ 28 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 29 ใ ์คํจ (3915.47ms, 10.8MB)
ํ
์คํธ 30 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
- ํ ์คํธ ์ผ์ด์ค 2๋ ์ฒ์๋ถํฐ ํฉ์ด ๊ฐ์ ๋, ์กฐ๊ฑด๋ฌธ ์์๋ง ๋ฐ๊พธ๋ฉด ๋ง์.
def solution(queue1, queue2):
answer = 0
lenQ = len(queue1) * len(queue2)
while answer < lenQ:
if sum(queue1) == sum(queue2):
return answer
if sum(queue1) > sum(queue2):
queue2.append(queue1.pop(0))
else:
queue1.append(queue2.pop(0))
answer += 1
return -1
- ๋๋จธ์ง ์๊ฐ ์ด๊ณผ๋ ์ด๋ป๊ฒ ํด๊ฒฐํด์ผ ํ๋...? sum ํจ์ ๋๋ฌธ์ ๋๋ฆฐ๊ฑด๊ฐ?
def solution(queue1, queue2):
answer = 0
lenQ = len(queue1) * len(queue2)
sum1 = sum(queue1)
sum2 = sum(queue2)
while answer < lenQ:
if sum1 == sum2:
return answer
if sum1 > sum2:
_tmp = queue1.pop(0)
sum1 -= _tmp
sum2 += _tmp
queue2.append(_tmp)
else:
_tmp = queue2.pop(0)
sum2 -= _tmp
sum1 += _tmp
queue1.append(_tmp)
answer += 1
return -1
- sum ํจ์ ์ ๊ฑฐํ๋๊น ๋นจ๋ผ์ก๋ค. ๊ทธ๋๋ ์๊ฐ์ด๊ณผ ๋ฐ์.
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.04ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.04ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.12ms, 10.3MB)
ํ
์คํธ 11 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 12 ใ ํต๊ณผ (160.01ms, 14MB)
ํ
์คํธ 13 ใ ํต๊ณผ (4.91ms, 11.1MB)
ํ
์คํธ 14 ใ ํต๊ณผ (4.31ms, 11.3MB)
ํ
์คํธ 15 ใ ํต๊ณผ (235.36ms, 17MB)
ํ
์คํธ 16 ใ ํต๊ณผ (29.30ms, 17.5MB)
ํ
์คํธ 17 ใ ํต๊ณผ (7.37ms, 16.4MB)
ํ
์คํธ 18 ใ ํต๊ณผ (77.45ms, 29.9MB)
ํ
์คํธ 19 ใ ํต๊ณผ (863.07ms, 34.3MB)
ํ
์คํธ 20 ใ ํต๊ณผ (7243.30ms, 34.7MB)
ํ
์คํธ 21 ใ ํต๊ณผ (3127.62ms, 35MB)
ํ
์คํธ 22 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 23 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 24 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 25 ใ ํต๊ณผ (0.04ms, 10.2MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.02ms, 10MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.4MB)
ํ
์คํธ 28 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 29 ใ ํต๊ณผ (0.10ms, 10.9MB)
ํ
์คํธ 30 ใ ํต๊ณผ (5550.23ms, 18.4MB)
- ํน์ lenQ๊ฐ ๋๋ฌด ๊ธธ์๋ ๊ฑธ๊น๋?
- ๋ต์ด ํ๋ฆฐ๊ฒ 1๊ฐ
- ์๊ฐ ์ด๊ณผ 3๊ฐ
def solution(queue1, queue2):
answer = 0
lenQ = (len(queue1) + len(queue2)) * 1.3 # lenQ๋ฅผ ์ค์ฌ๋ณด๊น?
sum1 = sum(queue1)
sum2 = sum(queue2)
while answer < lenQ:
if sum1 == sum2:
return answer
if sum1 > sum2:
_tmp = queue1.pop(0)
sum1 -= _tmp
sum2 += _tmp
queue2.append(_tmp)
else:
_tmp = queue2.pop(0)
sum2 -= _tmp
sum1 += _tmp
queue1.append(_tmp)
answer += 1
return -1
- ํ ์คํธ ์ผ์ด์ค 22, 23, 24 ์๊ฐ ์ด๊ณผ๋ก ์คํจ.
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.00ms, 10.1MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.06ms, 10.1MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.23ms, 10.2MB)
ํ
์คํธ 11 ใ ํต๊ณผ (952.51ms, 13.9MB)
ํ
์คํธ 12 ใ ํต๊ณผ (170.89ms, 13.9MB)
ํ
์คํธ 13 ใ ํต๊ณผ (4.26ms, 11.2MB)
ํ
์คํธ 14 ใ ํต๊ณผ (3.98ms, 11.4MB)
ํ
์คํธ 15 ใ ํต๊ณผ (223.09ms, 17.1MB)
ํ
์คํธ 16 ใ ํต๊ณผ (31.09ms, 17.6MB)
ํ
์คํธ 17 ใ ํต๊ณผ (6.57ms, 16.5MB)
ํ
์คํธ 18 ใ ํต๊ณผ (77.82ms, 29.8MB)
ํ
์คํธ 19 ใ ํต๊ณผ (868.39ms, 34.3MB)
ํ
์คํธ 20 ใ ํต๊ณผ (7123.35ms, 34.8MB)
ํ
์คํธ 21 ใ ํต๊ณผ (2941.93ms, 34.8MB)
ํ
์คํธ 22 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 23 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 24 ใ ์คํจ (์๊ฐ ์ด๊ณผ)
ํ
์คํธ 25 ใ ํต๊ณผ (0.05ms, 10.2MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 28 ใ ํต๊ณผ (7734.25ms, 18.3MB)
ํ
์คํธ 29 ใ ํต๊ณผ (0.11ms, 10.8MB)
ํ
์คํธ 30 ใ ํต๊ณผ (6124.55ms, 18.3MB)
- ์๊ฐ ์ด๊ณผ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด push, pop์ ์ฌ์ฉ์ํ๊ณ ์ธ๋ฑ์ค๋ก ์ฎ๊ฒจ๋ค๋๋ฉด์ ํฉ๋ง ๋น๊ตํ๋ ๋ฐฉ์์ผ๋ก ๊ณ ์ณค๋๋ ํ ์คํธ์ผ์ด์ค 1, 11๋ฒ ์คํจํ๋ค.
def solution(queue1, queue2):
answer = 0
ptr1, ptr2 = 0, 0
sum1, sum2 = sum(queue1), sum(queue2)
lenQ1, lenQ2 = len(queue1), len(queue2)
lenQ = (lenQ1 + lenQ2) * 1.3 # LenQ๋ฅผ ์ค์ฌ๋ณผ๊น?
if (sum1 + sum2) % 2 != 0:
return -1
while answer < lenQ:
if sum1 == sum2:
return answer
elif sum1 < sum2 and sum2 > 0:
_tmp = queue2[ptr2]
sum2 -= _tmp
sum1 += _tmp
ptr2 += 1
if ptr2 == lenQ2:
ptr2 = 0
elif sum1 > sum2 and sum1 > 0:
_tmp = queue1[ptr1]
sum1 -= _tmp
sum2 += _tmp
ptr1 += 1
if ptr1 == lenQ1:
ptr1 = 0
else:
answer += lenQ
answer += 1
return -1
- ํ์ฐธ์ ์ณ๋ค๋ณด๋ ๋ฌธ์ ์ ์ด ๋ณด์๋ค. ์คํจํ๋ ์ด์ ๋ ํ1์์ ๋ฝ์๋ธ ๊ฐ์ ํ2๋ก ๋ฃ์ด์ผ ํ๋๋ฐ, ์๋ ๋ฐฉ์์ ํฌ์ธํฐ๋ง ์ด๋ํ๊ณ ๋ค๋๊ธฐ ๋๋ฌธ์ ์๋ก ์์ด์ง ์์์ ์คํจใ
- ๊ทธ๋์ pop์ ์ํ๊ณ , append๋ง ํ๋๊ฑธ๋ก ๋ฐ์ชฝ๋ง ์ฌ์ฉํ๋ ๊ฑธ๋ก ์์ ํ๋๋ ์ฑ๊ณต. ์์ฌ์ด ๋ฐ์ชฝ์ง๋ฆฌ ์ฑ๊ณตใ
def solution(queue1, queue2):
answer = 0
lenQ = (len(queue1) + len(queue2)) * 1.3 # lenQ๋ฅผ ์ค์ฌ๋ณด๊น?
sum1 = sum(queue1)
sum2 = sum(queue2)
ptr1, ptr2 = 0, 0
while answer < lenQ:
if sum1 == sum2:
return answer
if sum1 > sum2:
_tmp = queue1[ptr1]
ptr1 += 1
sum1 -= _tmp
sum2 += _tmp
queue2.append(_tmp)
else:
_tmp = queue2[ptr2]
ptr2 += 1
sum2 -= _tmp
sum1 += _tmp
queue1.append(_tmp)
answer += 1
return -1
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.00ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.01ms, 10.4MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.04ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.09ms, 10.4MB)
ํ
์คํธ 11 ใ ํต๊ณผ (29.69ms, 14.9MB)
ํ
์คํธ 12 ใ ํต๊ณผ (11.30ms, 14MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.78ms, 11.1MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.85ms, 11.5MB)
ํ
์คํธ 15 ใ ํต๊ณผ (3.43ms, 17MB)
ํ
์คํธ 16 ใ ํต๊ณผ (1.29ms, 17.5MB)
ํ
์คํธ 17 ใ ํต๊ณผ (1.07ms, 16.6MB)
ํ
์คํธ 18 ใ ํต๊ณผ (3.25ms, 30MB)
ํ
์คํธ 19 ใ ํต๊ณผ (5.57ms, 34.3MB)
ํ
์คํธ 20 ใ ํต๊ณผ (22.14ms, 34.8MB)
ํ
์คํธ 21 ใ ํต๊ณผ (11.08ms, 35MB)
ํ
์คํธ 22 ใ ํต๊ณผ (52.15ms, 35.1MB)
ํ
์คํธ 23 ใ ํต๊ณผ (37.68ms, 36MB)
ํ
์คํธ 24 ใ ํต๊ณผ (61.73ms, 35.8MB)
ํ
์คํธ 25 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.02ms, 10.4MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 28 ใ ํต๊ณผ (80.32ms, 19.7MB)
ํ
์คํธ 29 ใ ํต๊ณผ (0.21ms, 10.9MB)
ํ
์คํธ 30 ใ ํต๊ณผ (47.23ms, 19.3MB)
๊ณ ์์ ํ์ด
- ์ ๋ฐํ๋ค. ์ด๋ป๊ฒ ํผ ๊ฑด์ง ๋ชจ๋ฅด๊ฒ ๋ค...
def solution(que1, que2):
queSum = (sum(que1) + sum(que2))
if queSum % 2:
return -1
target = queSum // 2
n = len(que1)
start = 0
end = n - 1
ans = 0
cur = sum(que1)
que3 = que1 + que2
while cur != target:
if cur < target:
end += 1
if end == n * 2:
return -1
cur += que3[end]
else:
cur -= que3[start]
start += 1
ans += 1
return ans
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.00ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.04ms, 10.4MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.09ms, 10.2MB)
ํ
์คํธ 11 ใ ํต๊ณผ (14.50ms, 14.5MB)
ํ
์คํธ 12 ใ ํต๊ณผ (4.48ms, 14.5MB)
ํ
์คํธ 13 ใ ํต๊ณผ (2.02ms, 12.2MB)
ํ
์คํธ 14 ใ ํต๊ณผ (2.24ms, 12.3MB)
ํ
์คํธ 15 ใ ํต๊ณผ (4.09ms, 18.1MB)
ํ
์คํธ 16 ใ ํต๊ณผ (2.99ms, 18.5MB)
ํ
์คํธ 17 ใ ํต๊ณผ (3.10ms, 17.5MB)
ํ
์คํธ 18 ใ ํต๊ณผ (10.80ms, 32.9MB)
ํ
์คํธ 19 ใ ํต๊ณผ (13.09ms, 37.2MB)
ํ
์คํธ 20 ใ ํต๊ณผ (21.46ms, 37.6MB)
ํ
์คํธ 21 ใ ํต๊ณผ (15.95ms, 37.6MB)
ํ
์คํธ 22 ใ ํต๊ณผ (40.42ms, 37.7MB)
ํ
์คํธ 23 ใ ํต๊ณผ (31.70ms, 37.6MB)
ํ
์คํธ 24 ใ ํต๊ณผ (48.96ms, 37.6MB)
ํ
์คํธ 25 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 28 ใ ํต๊ณผ (29.33ms, 19.1MB)
ํ
์คํธ 29 ใ ํต๊ณผ (0.45ms, 10.8MB)
ํ
์คํธ 30 ใ ํต๊ณผ (30.97ms, 19MB)
def solution(queue1, queue2):
indicator2=sum(queue1)-int(sum(queue1+queue2)/2)
answer=0
sub_list=(queue1+queue2+queue1)[::-1]
add_list=(queue2+queue1+queue2)[::-1]
while indicator2!=0:
try:
if indicator2>0:
indicator2-=sub_list.pop()
else:
indicator2+=add_list.pop()
except:
return -1
answer+=1
return answer
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.09ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.22ms, 10.5MB)
ํ
์คํธ 11 ใ ํต๊ณผ (38.68ms, 17.9MB)
ํ
์คํธ 12 ใ ํต๊ณผ (9.16ms, 17.8MB)
ํ
์คํธ 13 ใ ํต๊ณผ (6.98ms, 16.9MB)
ํ
์คํธ 14 ใ ํต๊ณผ (8.44ms, 18MB)
ํ
์คํธ 15 ใ ํต๊ณผ (13.42ms, 23.9MB)
ํ
์คํธ 16 ใ ํต๊ณผ (13.06ms, 24.8MB)
ํ
์คํธ 17 ใ ํต๊ณผ (13.17ms, 24.3MB)
ํ
์คํธ 18 ใ ํต๊ณผ (41.66ms, 53.6MB)
ํ
์คํธ 19 ใ ํต๊ณผ (53.17ms, 57.8MB)
ํ
์คํธ 20 ใ ํต๊ณผ (60.49ms, 58.3MB)
ํ
์คํธ 21 ใ ํต๊ณผ (65.42ms, 58.3MB)
ํ
์คํธ 22 ใ ํต๊ณผ (81.41ms, 58.3MB)
ํ
์คํธ 23 ใ ํต๊ณผ (66.32ms, 58.3MB)
ํ
์คํธ 24 ใ ํต๊ณผ (76.20ms, 58.4MB)
ํ
์คํธ 25 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 28 ใ ํต๊ณผ (74.54ms, 26MB)
ํ
์คํธ 29 ใ ํต๊ณผ (1.17ms, 11.6MB)
ํ
์คํธ 30 ใ ํต๊ณผ (34.34ms, 26MB)
from collections import deque
from itertools import combinations
def solution(queue1, queue2):
# 1. ํ ์๋ฃํ์ผ๋ก ๋ณ๊ฒฝ
q1 = deque(queue1); q2 = deque(queue2); totalq = q1 + q2
sum1 = sum(q1); sum2 = sum(q2); total_sum = (sum1+sum2)
target = total_sum/2
# 2. ๋ถ๊ฐ๋ฅํ ์ํฉ ์์ธ์ฒ๋ฆฌ : ์ ํํ ์ ๋ฐ์ผ๋ก ๋๋์ ์๊ฑฐ๋ ์ ๋ฐ ๊ฐ๋ณด๋ค ํฐ ์์๊ฐ ์กด์ฌํ๊ฑฐ๋
print(target)
if target != int(target) or [i for i in totalq if i > target]:
return -1
# if not any()
# 3. ์๋ฃ ์ฎ๊ธฐ๊ธฐ ๊ตฌํ
# First in First Out : ์์์ ๋ผ์ด ๋ค์ ๋ถ์ธ๋ค
# sum์ด target๋ณด๋ค ์์ผ๋ฉด ๋ถ์ด๊ณ , sum์ด target๋ณด๋ค ํฌ๋ฉด ์ฎ๊ฒจ์ค๋ค ๋ ๊ฒฝ์ฐ์ ๋ํด sum()์ ์ฐ์ง ๋ง๊ณ ์ถ๊ฐ์์๋ง ๋ํด์ฃผ๊ฑฐ๋ ๋บธ๋ค
cnt = 0
_sum = sum1
INF = 10000000
for _ in range(INF):
if _sum == target:
return cnt
elif _sum > target:
_sum -= q1[0]
q2.append(q1.popleft())
cnt += 1
elif _sum < target:
_sum += q2[0]
q1.append(q2.popleft())
cnt += 1
return -1
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.08ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.08ms, 10.2MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.16ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.55ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (2551.20ms, 15.4MB)
ํ
์คํธ 12 ใ ํต๊ณผ (12.22ms, 15.5MB)
ํ
์คํธ 13 ใ ํต๊ณผ (16.34ms, 13.2MB)
ํ
์คํธ 14 ใ ํต๊ณผ (10.68ms, 13.6MB)
ํ
์คํธ 15 ใ ํต๊ณผ (16.54ms, 19.5MB)
ํ
์คํธ 16 ใ ํต๊ณผ (20.22ms, 20.1MB)
ํ
์คํธ 17 ใ ํต๊ณผ (12.80ms, 19.3MB)
ํ
์คํธ 18 ใ ํต๊ณผ (61.55ms, 37.9MB)
ํ
์คํธ 19 ใ ํต๊ณผ (46.07ms, 42.2MB)
ํ
์คํธ 20 ใ ํต๊ณผ (67.96ms, 42.7MB)
ํ
์คํธ 21 ใ ํต๊ณผ (52.39ms, 42.7MB)
ํ
์คํธ 22 ใ ํต๊ณผ (115.27ms, 42.6MB)
ํ
์คํธ 23 ใ ํต๊ณผ (93.99ms, 42.7MB)
ํ
์คํธ 24 ใ ํต๊ณผ (114.76ms, 42.8MB)
ํ
์คํธ 25 ใ ํต๊ณผ (0.07ms, 10.2MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.05ms, 10.2MB)
ํ
์คํธ 28 ใ ํต๊ณผ (2415.11ms, 20.9MB)
ํ
์คํธ 29 ใ ํต๊ณผ (1.29ms, 11MB)
ํ
์คํธ 30 ใ ํต๊ณผ (74.32ms, 20.7MB)
- ํ๋ฅผ ํฉ์ณ์ ์ข ๋ ๋น ๋ฅด๊ฒ ํ ์ ์์ง ์์๊น?
def solution(queue1, queue2):
answer = 0
lenQ = (len(queue1) + len(queue2)) * 1.3 # lenQ๋ฅผ ์ค์ฌ๋ณด๊น?
sum1 = sum(queue1)
sum2 = sum(queue2)
if (sum1 + sum2) % 2 != 0:
return -1
ptr1, ptr2 = 0, 0
# ํ๋ฅผ ๋ฏธ๋ฆฌ ํฉ์ณ๋๋ฉด index๋ง ์ฎ๊ฒจ๋ค๋ ์ ์์ง๋กฑ
queue3 = queue1 + queue2 #+ queue1
queue4 = queue2 + queue1 #+ queue2
while answer < lenQ:
if sum1 == sum2:
return answer
if sum1 > sum2:
_tmp = queue3[ptr1]
ptr1 += 1
sum1 -= _tmp
sum2 += _tmp
answer += 1
if sum1 < sum2:
_tmp = queue4[ptr2]
ptr2 += 1
sum2 -= _tmp
sum1 += _tmp
answer += 1
return -1
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.08ms, 10.1MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.11ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (24.07ms, 15.2MB)
ํ
์คํธ 12 ใ ํต๊ณผ (5.80ms, 15.1MB)
ํ
์คํธ 13 ใ ํต๊ณผ (3.68ms, 13.3MB)
ํ
์คํธ 14 ใ ํต๊ณผ (2.67ms, 13.7MB)
ํ
์คํธ 15 ใ ํต๊ณผ (5.99ms, 19.3MB)
ํ
์คํธ 16 ใ ํต๊ณผ (4.84ms, 20.1MB)
ํ
์คํธ 17 ใ ํต๊ณผ (3.90ms, 18.9MB)
ํ
์คํธ 18 ใ ํต๊ณผ (15.02ms, 37.4MB)
ํ
์คํธ 19 ใ ํต๊ณผ (17.16ms, 41.7MB)
ํ
์คํธ 20 ใ ํต๊ณผ (30.09ms, 42.1MB)
ํ
์คํธ 21 ใ ํต๊ณผ (22.29ms, 42.1MB)
ํ
์คํธ 22 ใ ํต๊ณผ (51.58ms, 42.1MB)
ํ
์คํธ 23 ใ ํต๊ณผ (38.58ms, 42.3MB)
ํ
์คํธ 24 ใ ํต๊ณผ (57.11ms, 42.2MB)
ํ
์คํธ 25 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 26 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 28 ใ ํต๊ณผ (44.71ms, 20.5MB)
ํ
์คํธ 29 ใ ํต๊ณผ (0.36ms, 11.1MB)
ํ
์คํธ 30 ใ ํต๊ณผ (37.86ms, 20.6MB)
- ํ์ง๋ง ๊ณ ์์ ํ์ด๊ฐ ๋ ๋น ๋ฅด๋ค.
- ํ 2๊ฐ๋ฅผ ํฉ์ณ์ index๋ก ์ด๋ํ๋๊ฒ ๋ฉ๋ชจ๋ฆฌ๋ ๋ ์ ๊ฒ ์๋ชจํ๊ณ ๋น ๋ฅด๋ค.
- ๋์ ๋ด ์ฝ๋๋ ๋ณด๊ธฐ ์ฌ์์ ์ข์ง ์์? ใ ใ ใ
๊ณ ์์ ํ์ด | ๋์ ์ฝ๋ |
์ ํ์ฑ ํ
์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.1MB) ํ ์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.2MB) ํ ์คํธ 3 ใ ํต๊ณผ (0.00ms, 10.2MB) ํ ์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB) ํ ์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.2MB) ํ ์คํธ 6 ใ ํต๊ณผ (0.02ms, 10.3MB) ํ ์คํธ 7 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 8 ใ ํต๊ณผ (0.02ms, 10.3MB) ํ ์คํธ 9 ใ ํต๊ณผ (0.04ms, 10.4MB) ํ ์คํธ 10 ใ ํต๊ณผ (0.09ms, 10.2MB) ํ ์คํธ 11 ใ ํต๊ณผ (14.50ms, 14.5MB) ํ ์คํธ 12 ใ ํต๊ณผ (4.48ms, 14.5MB) ํ ์คํธ 13 ใ ํต๊ณผ (2.02ms, 12.2MB) ํ ์คํธ 14 ใ ํต๊ณผ (2.24ms, 12.3MB) ํ ์คํธ 15 ใ ํต๊ณผ (4.09ms, 18.1MB) ํ ์คํธ 16 ใ ํต๊ณผ (2.99ms, 18.5MB) ํ ์คํธ 17 ใ ํต๊ณผ (3.10ms, 17.5MB) ํ ์คํธ 18 ใ ํต๊ณผ (10.80ms, 32.9MB) ํ ์คํธ 19 ใ ํต๊ณผ (13.09ms, 37.2MB) ํ ์คํธ 20 ใ ํต๊ณผ (21.46ms, 37.6MB) ํ ์คํธ 21 ใ ํต๊ณผ (15.95ms, 37.6MB) ํ ์คํธ 22 ใ ํต๊ณผ (40.42ms, 37.7MB) ํ ์คํธ 23 ใ ํต๊ณผ (31.70ms, 37.6MB) ํ ์คํธ 24 ใ ํต๊ณผ (48.96ms, 37.6MB) ํ ์คํธ 25 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 26 ใ ํต๊ณผ (0.02ms, 10.2MB) ํ ์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.2MB) ํ ์คํธ 28 ใ ํต๊ณผ (29.33ms, 19.1MB) ํ ์คํธ 29 ใ ํต๊ณผ (0.45ms, 10.8MB) ํ ์คํธ 30 ใ ํต๊ณผ (30.97ms, 19MB) |
์ ํ์ฑ ํ
์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.1MB) ํ ์คํธ 2 ใ ํต๊ณผ (0.00ms, 10.1MB) ํ ์คํธ 3 ใ ํต๊ณผ (0.01ms, 10.2MB) ํ ์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB) ํ ์คํธ 5 ใ ํต๊ณผ (0.02ms, 10.2MB) ํ ์คํธ 6 ใ ํต๊ณผ (0.02ms, 10.1MB) ํ ์คํธ 7 ใ ํต๊ณผ (0.02ms, 10.2MB) ํ ์คํธ 8 ใ ํต๊ณผ (0.03ms, 10.2MB) ํ ์คํธ 9 ใ ํต๊ณผ (0.08ms, 10.1MB) ํ ์คํธ 10 ใ ํต๊ณผ (0.11ms, 10.3MB) ํ ์คํธ 11 ใ ํต๊ณผ (24.07ms, 15.2MB) ํ ์คํธ 12 ใ ํต๊ณผ (5.80ms, 15.1MB) ํ ์คํธ 13 ใ ํต๊ณผ (3.68ms, 13.3MB) ํ ์คํธ 14 ใ ํต๊ณผ (2.67ms, 13.7MB) ํ ์คํธ 15 ใ ํต๊ณผ (5.99ms, 19.3MB) ํ ์คํธ 16 ใ ํต๊ณผ (4.84ms, 20.1MB) ํ ์คํธ 17 ใ ํต๊ณผ (3.90ms, 18.9MB) ํ ์คํธ 18 ใ ํต๊ณผ (15.02ms, 37.4MB) ํ ์คํธ 19 ใ ํต๊ณผ (17.16ms, 41.7MB) ํ ์คํธ 20 ใ ํต๊ณผ (30.09ms, 42.1MB) ํ ์คํธ 21 ใ ํต๊ณผ (22.29ms, 42.1MB) ํ ์คํธ 22 ใ ํต๊ณผ (51.58ms, 42.1MB) ํ ์คํธ 23 ใ ํต๊ณผ (38.58ms, 42.3MB) ํ ์คํธ 24 ใ ํต๊ณผ (57.11ms, 42.2MB) ํ ์คํธ 25 ใ ํต๊ณผ (0.03ms, 10.2MB) ํ ์คํธ 26 ใ ํต๊ณผ (0.03ms, 10.2MB) ํ ์คํธ 27 ใ ํต๊ณผ (0.02ms, 10.1MB) ํ ์คํธ 28 ใ ํต๊ณผ (44.71ms, 20.5MB) ํ ์คํธ 29 ใ ํต๊ณผ (0.36ms, 11.1MB) ํ ์คํธ 30 ใ ํต๊ณผ (37.86ms, 20.6MB) |
728x90
๋ฐ์ํ
'๊ฒ์ ํ๋ก๊ทธ๋๋ฐ > Python ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค 2 x n ํ์ผ๋ง (0) | 2023.02.16 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค ๊ฐ์ฅ ํฐ ์ ์ฌ๊ฐํ ์ฐพ๊ธฐ (0) | 2023.02.16 |
ํ๋ก๊ทธ๋๋จธ์ค ์ฐ๋ฐ์์ด ์ ์ ๋ถ (0) | 2023.02.15 |
ํ๋ก๊ทธ๋๋จธ์ค ์ซ์ ์นด๋ ๋๋๊ธฐ (0) | 2023.02.15 |
ํ๋ก๊ทธ๋๋จธ์ค ํ ์ด๋ธ ํด์ ํจ์ (0) | 2023.02.15 |