-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookie.java
37 lines (28 loc) · 880 Bytes
/
Cookie.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
package day4;
import java.util.HashMap;
//child with most cookies
class Cookie {
public int maximumUniqueSubarray(int[] nums) {
int p1 = 0;
int maxSum = Integer.MIN_VALUE;
int l = nums.length;
int sum = 0;
HashMap<Integer, Integer> map = new HashMap<>();
for (int p2 = 0; p2 < l; p2++) {
map.put(nums[p2], map.getOrDefault(nums[p2], 0) + 1);
sum += nums[p2];
if (map.get(nums[p2]) > 1) {
sum -= nums[p1];
map.put(nums[p1], map.get(nums[p1] - 1));
p1++;
}
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}
public static void main(String[] args) {
Cookie s = new Cookie();
int[] nums = { 4, 2, 4, 5, 6 };
System.out.println(s.maximumUniqueSubarray(nums));
}
}