public int solution(int[] queue1, int[] queue2) {
Queue<Integer> q1 = new ArrayDeque<>();
Queue<Integer> q2 = new ArrayDeque<>();
long sum1 = 0;
long sum2 = 0;
for (int tmp : queue1) {
q1.add(tmp);
sum1 += tmp;
}
for (int tmp : queue2) {
q2.add(tmp);
sum2 += tmp;
}
long totalSum = sum1 + sum2;
if (totalSum % 2 != 0) {
return -1;
}
long targetSum = totalSum / 2;
int cnt1 = 0;
int cnt2 = 0;
int limit = queue1.length * 2;
while (cnt1 <= limit && cnt2 <= limit) {
if (sum1 == targetSum) {
return cnt1 + cnt2;
}
if (sum1 > targetSum) {
int temp = q1.poll();
sum1 -= temp;
sum2 += temp;
q2.add(temp);
cnt1++;
} else {
int temp = q2.poll();
sum2 -= temp;
sum1 += temp;
q1.add(temp);
cnt2++;
}
}
return -1;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/118667