Skip to content

Commit 038223e

Browse files
authored
Merge pull request #1210 from AlgorithmWithGod/0224LJH
[20251023] PGM/ Lv2 / 두 큐 합 같게 하기 / 이종환
2 parents 5afc08d + 91d7216 commit 038223e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
class Solution {
6+
public int solution(int[] queue1, int[] queue2) {
7+
int[] arr = new int[queue1.length + queue2.length];
8+
int len = arr.length;
9+
for (int i = 0; i < queue1.length; i++){
10+
arr[i] = queue1[i];
11+
}
12+
for (int i = 0; i < queue2.length; i++){
13+
arr[queue1.length+i] = queue2[i];
14+
}
15+
16+
long total = 0;
17+
long goal = 0;
18+
long cur = arr[0];
19+
int start = 0;
20+
int end = 0;
21+
22+
for (int i = 0; i <len; i++ ) total += arr[i];
23+
if (total %2 != 0) return -1;
24+
25+
goal = total/2;
26+
27+
int answer = Integer.MAX_VALUE;
28+
29+
while(start <= end && end < len){
30+
if (cur == goal) {
31+
32+
if ( end < queue1.length-1){
33+
answer = Math.min(answer, start + end+1 +queue2.length);
34+
35+
} else{
36+
answer = Math.min(answer,start + end - queue1.length+1);
37+
}
38+
end++;
39+
if (end >= len) break;
40+
cur+=arr[end];
41+
42+
43+
} else if (cur < goal){
44+
end++;
45+
if (end >= len) break;
46+
cur+=arr[end];
47+
} else{
48+
if (arr[start] > goal) return -1;
49+
50+
cur-=arr[start];
51+
start++;
52+
}
53+
}
54+
55+
if (answer == Integer.MAX_VALUE) answer = -1;
56+
57+
58+
59+
return answer;
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)