forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1695.java
56 lines (53 loc) · 1.78 KB
/
_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
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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
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;
}
}
public static class Solution2 {
/**
* My completely original solution on 10/202/2021. Classic sliding window solution.
*/
public int maximumUniqueSubarray(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
int start = 0;
int ans = 0;
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
Integer lastIndex = map.get(nums[i]);
while (start <= lastIndex) {
sum -= nums[start];
start++;
}
}
sum += nums[i];
map.put(nums[i], i);
ans = Math.max(ans, sum);
}
return ans;
}
}
}