We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ed12163 commit 7e3416fCopy full SHA for 7e3416f
C++/maximum-erasure-value.cpp
@@ -0,0 +1,20 @@
1
+// Time: O(n)
2
+// Space: O(n)
3
+
4
+class Solution {
5
+public:
6
+ int maximumUniqueSubarray(vector<int>& nums) {
7
+ unordered_map<int, int> lookup;
8
+ int prefix[size(nums) + 1];
9
+ int result = 0, left = 0;
10
+ for (int right = 0; right < size(nums); ++right) {
11
+ prefix[right + 1] = prefix[right] + nums[right];
12
+ if (lookup.count(nums[right])) {
13
+ left = max(left, lookup[nums[right]] + 1);
14
+ }
15
+ lookup[nums[right]] = right;
16
+ result = max(result, prefix[right + 1] - prefix[left]);
17
18
+ return result;
19
20
+};
0 commit comments