-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
두 큐 합 같게 만들기 - KaKao Teach Intership - level2
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |