Skip to content

Conversation

@LiiNi-coder
Copy link
Contributor

@LiiNi-coder LiiNi-coder commented Aug 20, 2025

🧷 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/118667

🧭 풀이 시간

50 분

👀 체감 난이도

✏️ 문제 설명

  • 두 큐가 존재하고, 큐의 한쪽에서 pop한 것을 다룬 큐에 insert하는 행동을 a라 할때, a를 최소 몇번을 해야 두 큐의 합이 각각 같게 만드는지 구하는 문제

🔍 풀이 방법

  • BFS를 진행하되, BFS의 Element로 두 큐의 상태를 넣었음

⏳ 회고

  • 이 방식이 매 BFS시 큐를 아예 복사하는 것이라서 비효율적인것은 알지만, 제한된 시간내에 풀려하다 보니 급하게 푸느라, 이 방법대로 계속 해보려고 다른 풀이나 gpt힘을 빌려서 visited도 도입하는 등 풀어보려했습니다만 여전히 효율성체크에서 에러납니다...
  • 이런 문제에서 처음에 생각을 할때, 비효율적이다 싶으면 좀더 생각하는 시간을 가지고 구현에 임하는 버릇을 들이자. 투포인터를 쓴다는 생각을 못해서 이 사단이 났따... 실제 코테에서 이러진 말자

@ShinHeeEul ShinHeeEul merged commit 2ee75f3 into main Aug 20, 2025
1 check passed
@LiiNi-coder
Copy link
Contributor Author

늦어서 여기에 달겠습니다...

@LiiNi-coder LiiNi-coder added the fail 😢 해설을 보고 풀었거나, 못 풀었을 때 label Aug 20, 2025
@LiiNi-coder
Copy link
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;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fail 😢 해설을 보고 풀었거나, 못 풀었을 때

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants