-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1695.java
More file actions
25 lines (23 loc) · 856 Bytes
/
prblm1695.java
File metadata and controls
25 lines (23 loc) · 856 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
import java.util.*;
public class prblm1695 {
public static void main(String[] args) {
int[] nums = {4,2,4,5,6};
System.out.println(new prblm1695().maximumUniqueSubarray(nums));
}
public int maximumUniqueSubarray(int[] nums) {
int start = 0, wSum = 0, answer = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int end = 0; end < nums.length; end++){
wSum += nums[end];
map.put(nums[end], map.getOrDefault(nums[end], 0) + 1);
while(map.size() >= end - start + 1){
answer = Math.max(answer, wSum);
wSum -= nums[start];
map.put(nums[start], map.get(nums[start]) - 1);
if(map.get(nums[start]) == 0) map.remove(nums[start]);
start++;
}
}
return answer;
}
}