[C++] 76ms, Fastest Solution to Date Explained, 100% Time, ~90% Space
This problem is clearly a sliding window problem, with the extra twist of a variable size of the window.\n\nTo solve it, we will need to build a running total o
This problem is clearly a sliding window problem, with the extra twist of a variable size of the window.\n\nTo solve it, we will need to build a running total of all the values in the input, minus the total up to the last repeated element we encountered.\n\nThis would call for a hashmap (or an `unordered_map` in C++), but since hashing does not come for free and not all contant times are equal, not to mention how expensive in terms of space a map might be, it turns out that even justs brutally creating an array of `10001` elements works just fine - faster even when you go to initialise all its cells once and even saves more memory than most implementations!\n\nTo do so we will need a few support variables:\n* `len` will store the length of the initial input;\n* `lastSeenAt` is an array of `10001` elements where we will store the position of were we last saw each value;\n* `sumUpTo` is where we store our ongoing sum;\n* `res` will store our best result so far and will be initialised to be `nums[0]`;\n* `lastRemoved` will store the position of the last element we removed the sum from (and thus be the beginning of our window).\n\nWe will then set all the values in `lastSeen` to be `-1`, and set the first slot in `sumUpto` and `lastSeenAt[nums[0]]` to be `nums[0]` and `0`, respectively, just to avoid an extra check in the upcoming loop where we will always refer to the previous value.\n\nTime then to loop with `i` from `1` to `len` (excluded) and:\n* assign the current value (`nums[i]`) to `n`;\n* compute the current cell of `sumUpTo` as the sum of the previous plus `n`;\n* update `lastRemoved` as the largest between its current value and the last position in which we saw `n` (`lastSeen[n]`);\n* update `res` as the maximum between its current value and the current running sum (`sumUpTo[i]`), minus the sum of all elements up to `lastRemoved`, provided we ever encountered a single duplicated value (`lastRemoved != -1`);\n* update `lastSeen[n]` to be the current position of `n`: `i`.\n\nOnce done, we can return `res` :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    int maximumUniqueSubarray(vector<int>& nums) {\n        int len = nums.size(), lastSeenAt[10000 + 1], sumUpTo[len], res = nums[0], lastRemoved = -1;\n        // preparing lastSeenAt\n        fill_n(lastSeenAt, 10000 + 1, -1);\n        // parsing the first value\n        sumUpTo[0] = nums[0];\n        lastSeenAt[nums[0]] = 0;\n        // parsing all the other value\n        for (int i = 1, n; i < len; i++) {\n            n = nums[i];\n            sumUpTo[i] = sumUpTo[i - 1] + n;\n            lastRemoved = max(lastRemoved, lastSeenAt[n]);\n            res = max(res, sumUpTo[i] - (lastRemoved != -1 ? sumUpTo[lastRemoved] : 0));\n            lastSeenAt[n] = i;\n        }\n        return res;\n    }\n};\n```\n\nThe brag:\n![image](https://assets.leetcode.com/users/images/2b5ec043-9d12-4a5f-bb64-0a9bd7d0e683_1622250991.191319.png)\n
['C', 'C++']