-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1695.java
More file actions
30 lines (27 loc) · 916 Bytes
/
_1695.java
File metadata and controls
30 lines (27 loc) · 916 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
26
27
28
29
30
package com.github.aditya;
import java.util.HashSet;
import java.util.Set;
public class _1695 {
// 61 ms, faster than 84.55%, memory 52.3 MB less than 93.09%
// Time Complexity O(n) and Space Complexity O(m) m = number of unique elements in array.
class Solution {
public int maximumUniqueSubarray(int[] nums) {
int i = 0, j = 0;
int maxSum = 0, currentSum = 0;
Set<Integer> set = new HashSet<>();
while (j < nums.length) {
if (!set.contains(nums[j])) {
set.add(nums[j]);
currentSum += nums[j];
j++;
maxSum = Math.max(maxSum, currentSum);
} else {
set.remove(nums[i]);
currentSum -= nums[i];
i++;
}
}
return maxSum;
}
}
}