File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments