Maximum Erasure Value LeetCode solution in C++

1
class Solution {
2
public:
3
int maximumUniqueSubarray(vector<int> &nums) {
4
int curr_sum = 0, res = 0;
5

6
// set to store the elements
7
unordered_set<int> st;
8

9
int i = 0, j = 0;
10
while (j < nums.size()) {
11
while (st.count(nums[j]) > 0) {
12
// Removing the ith element untill we reach the repeating element
13
st.erase(nums[i]);
14
curr_sum -= nums[i];
15
i++;
16
}
17
// Add the current element to set and curr_sum value
18
curr_sum += nums[j];
19
st.insert(nums[j++]);
20

21
// res variable to keep track of largest curr_sum encountered till now...
22
res = max(res, curr_sum);
23
}
24

25
return res;
26
}
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0