-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwitterLongestPhase.java
More file actions
37 lines (32 loc) · 971 Bytes
/
TwitterLongestPhase.java
File metadata and controls
37 lines (32 loc) · 971 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
30
31
32
33
34
35
36
37
import java.util.LinkedList;
import java.util.Queue;
public class TwitterLongestPhase {
public int maxLength(int[] a, int k) {
if (a == null || a.length == 0) {
return 0;
}
Queue<Integer> queue = new LinkedList<Integer>();
int sum = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
queue.offer(a[i]);
sum += a[i];
while (sum > k) {
sum -= queue.poll();
}
if (sum == k) {
max = Math.max(max, queue.size());
}
}
return max;
}
public static void main(String[] args) {
TwitterLongestPhase tlp = new TwitterLongestPhase();
int[] a1 = {1, 2, 3};
int k1 = 3;
int[] a2 = {1, 2, 4, 1, 2, 3};
int k2 = 6;
System.out.println(tlp.maxLength(a1, k1));
System.out.println(tlp.maxLength(a2, k2));
}
}