-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_178870.java
More file actions
29 lines (27 loc) · 766 Bytes
/
_178870.java
File metadata and controls
29 lines (27 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int[] solution(int[] sequence, int k) {
int left = 0;
int right = 0;
int length = Integer.MAX_VALUE;
int sum = sequence[0];
int[] answer = new int[2];
while(true){
// System.out.println(left+"~"+right+":"+sum);
if(sum==k && (length > right-left)){
answer[0]=left;
answer[1]=right;
length = right-left;
}
if(sum<k){
right++;
if(right==sequence.length) break;
sum+=sequence[right];
}
else{
sum-=sequence[left];
left++;
}
}
return answer;
}
}