forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1695.java
29 lines (27 loc) · 944 Bytes
/
_1695.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
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.Set;
public class _1695 {
public static class Solution1 {
public int maximumUniqueSubarray(int[] nums) {
Set<Integer> set = new HashSet<>();
int maxSum = 0;
int runningSum = 0;
for (int right = 0, left = 0; right < nums.length; right++) {
if (set.add(nums[right])) {
runningSum += nums[right];
maxSum = Math.max(maxSum, runningSum);
} else {
while (left < right && set.contains(nums[right])) {
set.remove(nums[left]);
runningSum -= nums[left];
left++;
}
set.add(nums[right]);
runningSum += nums[right];
}
}
return maxSum;
}
}
}