-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiverseSubarray.java
77 lines (65 loc) · 1.66 KB
/
DiverseSubarray.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.*;
import java.io.*;
class DiverseSubarray {
int N, S, res;
int[] list;
DiverseSubarray(int _N, int _S, int[] _list) {
N = _N;
S = _S;
list = _list;
res = 0;
}
void solve() {
int[] changeList = new int[N];
//Map<type, count>
Map<Integer, Integer> counter = new HashMap<>();
for (int i = 0; i < N; i++) {
int curType = list[i];
if (!counter.containsKey(curType)) {
changeList[i] = 1;
counter.put(curType, 1);
} else {
if (counter.get(curType) == null) {changeList[i] = 0; }
else if (counter.get(curType) == S) {
changeList[i] = 0 - S;
counter.put(curType, null);
} else {
changeList[i] = 1;
counter.put(curType, counter.get(curType) + 1);
}
}
}
Jren.p(changeList);
res = maxSubarraySum(changeList);
}
int maxSubarraySum(int[] nums) {
int max = nums[0];
int preMax = nums[0];
for (int i = 1; i < nums.length; i++) {
int curMax;
if (preMax >= 0) {
curMax = preMax + nums[i];
} else {
curMax = nums[i];
}
max = Math.max(max, curMax);
}
return max;
}
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int T = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
for (int t = 1; t <= T; ++t) {
int N = in.nextInt();
int S = in.nextInt();
int[] list = new int[N];
for (int i = 0; i < N; i++) {
list[i] = in.nextInt();
}
DiverseSubarray ds = new DiverseSubarray(N, S, list);
ds.solve();
//output result
System.out.println("Case #" + t + ": " + ds.res);
}
}
}