[20250820] PGM / LV2 / 두 큐 합 같게 만들기 / 이인희#703
Merged
ShinHeeEul merged 1 commit intomainfrom Aug 20, 2025
Merged
Conversation
Contributor
Author
|
늦어서 여기에 달겠습니다... |
Contributor
Author
import java.util.*;
class Solution {
static class BfsQElement{
int count;
List<ArrayDeque<Integer>> qs;
BfsQElement(int count, ArrayDeque<Integer> q1, ArrayDeque<Integer> q2){
this.count = count;
this.qs = new ArrayList<ArrayDeque<Integer>>();
this.qs.add(q1);
this.qs.add(q2);
}
}
public int solution(int[] queue1, int[] queue2) {
int answer = 0;
var q1 = new ArrayDeque<Integer>();
var q2 = new ArrayDeque<Integer>();
long idealSum = 0L;
for(int i : queue1){
q1.offer(i);
idealSum += i;
}
for(int i : queue2){
q2.offer(i);
idealSum += i;
}
idealSum /= 2;
var bfsQ = new ArrayDeque<BfsQElement>();
var visited = new HashSet<Long>();
bfsQ.offer(new BfsQElement(0, q1, q2));
boolean isFind = false;
while(!bfsQ.isEmpty()){
BfsQElement e = bfsQ.poll();
int count = e.count;
long sum = getSum(e.qs.get(0));
visited.add(sum);
if(sum == idealSum){
answer = count;
isFind = true;
break;
}
var newQ1 = e.qs.get(0).clone();
var newQ2 = e.qs.get(1).clone();
var temp = newQ1.poll();
if(temp != null && !visited.contains(getSum(newQ1))){
newQ2.offer(temp);
bfsQ.add(new BfsQElement(count+1, newQ1, newQ2));
}
newQ1 = e.qs.get(1).clone();
newQ2 = e.qs.get(0).clone();
temp = newQ2.poll();
if(temp != null && !visited.contains(getSum(newQ2))){
newQ1.offer(temp);
bfsQ.add(new BfsQElement(count+1, newQ1, newQ2));
}
}
return (isFind)? answer : -1;
}
// private ArrayDeque<BfsQElement> getCloneOf(ArrayDeque<Integer> q){
// return
// }
private long getSum(ArrayDeque<Integer> q){
long sum = 0L;
for(int i : q){
sum += (long) i;
}
return sum;
}
} |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/118667
🧭 풀이 시간
50 분
👀 체감 난이도
✏️ 문제 설명
🔍 풀이 방법
⏳ 회고