Skip to content

Commit b7624dd

Browse files
authored
[20251017] PGM / LV2 / 두 큐 합 같게 만들기 / 강신지
1 parent 7f719e7 commit b7624dd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int solution(int[] queue1, int[] queue2) {
6+
int n = queue1.length;
7+
int totalSize = n*2;
8+
9+
long queueValue = 0, totalValue = 0;
10+
int[] nums = new int[totalSize];
11+
12+
for (int i = 0; i < n; i++) {
13+
nums[i] = queue1[i];
14+
queueValue += queue1[i];
15+
totalValue += queue1[i];
16+
}
17+
for (int i = 0; i < n; i++) {
18+
nums[n + i] = queue2[i];
19+
totalValue += queue2[i];
20+
}
21+
22+
// 전체 합이 홀수면 불가능
23+
if (totalValue % 2 == 1) return -1;
24+
long half = totalValue / 2;
25+
26+
if (queueValue == half) return 0;
27+
28+
int i = 0; // q1 시작 위치
29+
int j = n; // q2 시작 위치
30+
int moves = 0;
31+
32+
// 각 포인터는 최대 L까지 갈 수 있음
33+
while (i < totalSize && j < totalSize) {
34+
if (queueValue == half) return moves;
35+
36+
if (queueValue > half) {
37+
queueValue -= nums[i++];
38+
} else {
39+
queueValue += nums[j++];
40+
}
41+
moves++;
42+
}
43+
return -1;
44+
}
45+
}
46+
```

0 commit comments

Comments
 (0)