import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
// 두 큐 합 같게 만들기
public static int solution(int[] queue1, int[] queue2) {
int answer = 0;
int length = queue1.length;
long queue1Sum = 0; // queue1의 합계만을 사용할 것
long totalSum = 0;
long targetSum = 0;
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> q2 = new LinkedList<>();
queue1Sum = Arrays.stream(queue1).sum(); // 6
totalSum = queue1Sum + Arrays.stream(queue2).sum(); // 20
if (totalSum % 2 == 1) return -1; // 똑같이 나눌 수 없다면 -1 리턴
targetSum = totalSum / 2; // 10 // 하나의 큐만 절반 값을 맞춘다면 okay, 절반 값을 이용
for (int i = 0; i < length; i++) {
q1.offer(queue1[i]); // 1, 2, 1, 2
q2.offer(queue2[i]); // 1, 10, 1, 2
}
int tempNum = 0;
while (queue1Sum != targetSum) {
if (queue1Sum < targetSum) {
tempNum = q2.poll();
q1.offer(tempNum);
queue1Sum += tempNum;
} else {
tempNum = q1.poll();
q2.offer(tempNum);
queue1Sum -= tempNum;
}
answer++;
// queue1, queue2의 모든 원소가 자리바꿈하여 다시 원래의 위치로 오기 위한 횟수 (queue1.length + queue2.length) * 2 = 16
// 즉 16이 된다는 것은 다시 처음의 경우와 같아졌음을 의미하고, 더이상 반복할 필요가 없음을 뜻한다.
if (answer > length * 4 - 1) return -1;
}
return answer;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] queue1 = {1, 2, 1, 2};
int[] queue2 = {1, 10, 1, 2};
System.out.println(solution(queue1, queue2)); // 7
}
}
프로그래머스 두 큐 합 같게 만들기 문제 풀이 Java
'Java > 프로그래머스' 카테고리의 다른 글
[Java] 프로그래머스 [Level-2] 구명보트 (0) | 2022.11.27 |
---|---|
[Java] 프로그래머스 [Level-4] 행렬과 연산 (0) | 2022.11.27 |
[Java] 프로그래머스 [Level-1] 성격 유형 검사하기 (0) | 2022.11.27 |
[Java] 프로그래머스 [Level-4] 도둑질 (0) | 2022.11.27 |
[Java] 프로그래머스 [Level-4] [3차]파괴되지 않은 건물 (0) | 2022.11.27 |