Skip to content

Commit 3d6233f

Browse files
authored
Merge pull request #1069 from AlgorithmWithGod/LiiNi-coder
[20251008] PGM / LV2 / 연속된 부분 수열의 합 / 이인희
2 parents 90be671 + a565ba8 commit 3d6233f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int[] solution(int[] sequence, int k) {
6+
int[] answer = {-1, -1};
7+
int l = 0, r = 0, sum = 0;
8+
Deque<Integer> q = new ArrayDeque<>();
9+
while(r <= sequence.length){
10+
if(sum == k){
11+
if(answer[0] == -1 || (r - l - 1) < (answer[1] - answer[0])){
12+
answer[0] = l;
13+
answer[1] = r -1;
14+
}
15+
}
16+
17+
18+
if(sum >= k){
19+
sum -= q.poll();
20+
l++;
21+
}else{
22+
if(r == sequence.length)
23+
break;
24+
sum += sequence[r];
25+
q.add(sequence[r]);
26+
r++;
27+
}
28+
}
29+
return answer;
30+
}
31+
}
32+
33+
```

0 commit comments

Comments
 (0)