Skip to content

Commit

Permalink
두 큐 합 같게 만들기 - KaKao Teach Intership - level2
Browse files Browse the repository at this point in the history
  • Loading branch information
sey2 authored Aug 22, 2022
1 parent 8d14d64 commit 18887ff
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions programmers/QueueSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.LinkedList;
import java.util.Queue;
class Solution {
public int solution(int[] queue1, int[] queue2) {
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> q2 = new LinkedList<>();

long q1Sum = 0;
long q2Sum = 0;
for(int i=0; i<queue1.length; i++){
q1.add(queue1[i]);
q1Sum += queue1[i];

q2.add(queue2[i]);
q2Sum += queue2[i];
}

long mid = (q1Sum + q2Sum) / 2;

int answer = 0;
for(int i=0; i< queue1.length*3; i++){
if(q1Sum > q2Sum){
q1Sum -= q1.peek();
q2Sum += q1.peek();
q2.add(q1.poll());
answer++;
}else if(q1Sum < q2Sum){
q1Sum += q2.peek();
q2Sum -= q2.peek();
q1.add(q2.poll());
answer++;
}

if(q1Sum == mid && q2Sum == mid) return answer;
}

return -1;
}
}

0 comments on commit 18887ff

Please sign in to comment.