-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1695.cpp
More file actions
37 lines (27 loc) · 730 Bytes
/
1695.cpp
File metadata and controls
37 lines (27 loc) · 730 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
31
32
33
34
35
#include <algorithm>
#include<iostream>
#include<vector>
#include<unordered_map>
int maximumUniqueSubarray(std::vector<int>& nums) {
std::unordered_map<int, int> freq;
int maxSum = 0;
int tempSum = 0;
int left = 0;
for (int i = 0; i < nums.size(); ++i) {
++freq[nums[i]];
while (freq[nums[i]] > 1) {
--freq[nums[left]];
tempSum -= nums[left];
++left;
}
tempSum += nums[i];
maxSum = std::max(maxSum, tempSum);
}
return maxSum;
}
int main() {
// std::vector<int> nums = { 4,2,4,5,6 };
std::vector<int> nums = { 3,2,5,21,34,5,22,90,323,222 };
std::cout << maximumUniqueSubarray(nums);
std::cin.get();
}