JS, Python, Java, C++ | Easy 2-Pointer Sliding Window Solution w/ Explanation
(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\n\n---\n\n#### Idea:\n
*(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful,* ***please upvote*** *this post.)*\n\n---\n\n#### ***Idea:***\n\nLike most problems that ask about a contiguous subarray, this problem naturally calls for a **2-pointer sliding window** approach. There are a few ways we can keep track of the contents of the sliding window, but since the constraint upon **nums[i]** is fairly small, we can use the faster **arraymap** (**nmap**) method, rather than a **hashmap**.\n\nSo as we iterate our sliding window through **nums**, we\'ll move our **right** pointer forward, increasing the counter for the appropriate number in **nmap**. If that bucket in **nmap** goes above **1**, then we know that the newly-added number isn\'t unique in our sliding window, so we need to increase the **left** pointer until the counter is reduced back to **1**.\n\nWe should also, of course, keep track of the sum **total** of the sliding window. At each iteration, once we\'ve confirmed the uniqueness of the contents of the sliding window, we should also update our **best** result so far. Then, once we\'re done, we can just **return best**.\n\n - _**Time Complexity: O(N)** where **N** is the length of **nums**_\n - _**Space Complexity: O(10001)** for **nmap** keeping track of numbers from **0** to **10^4**_\n\n---\n\n#### ***Javascript Code:***\n\nThe best result for the code below is **96ms / 49.4MB** (beats 100% / 100%).\n```javascript\nvar maximumUniqueSubarray = function(nums) {\n    let nmap = new Int8Array(10001), total = 0, best = 0\n    for (let left = 0, right = 0; right < nums.length; right++) {\n        nmap[nums[right]]++, total += nums[right]\n        while (nmap[nums[right]] > 1)\n            nmap[nums[left]]--, total -= nums[left++]\n        best = Math.max(best, total)\n    }\n    return best\n};\n```\n\n---\n\n#### ***Python Code:***\n\nThe best result for the code below is **1180ms / 28.2MB** (beats 90% / 63%).\n```python\nclass Solution:\n    def maximumUniqueSubarray(self, nums: List[int]) -> int:\n        nmap, total, best, left = [0] * 10001, 0, 0, 0\n        for right in nums:\n            nmap[right] += 1\n            total += right\n            while nmap[right] > 1:\n                nmap[nums[left]] -= 1\n                total -= nums[left]\n                left += 1\n            best = max(best, total)\n        return best\n```\n\n---\n\n#### ***Java Code:***\n\nThe best result for the code below is **6ms / 47.5MB** (beats 97% / 100%).\n```java\nclass Solution {\n    public int maximumUniqueSubarray(int[] nums) {\n        short[] nmap = new short[10001];\n        int total = 0, best = 0;\n        for (int left = 0, right = 0; right < nums.length; right++) {\n            nmap[nums[right]]++;\n            total += nums[right];\n            while (nmap[nums[right]] > 1) {\n                nmap[nums[left]]--;\n                total -= nums[left++];\n            }\n            best = Math.max(best, total);\n        }\n        return best;\n    }\n}\n```\n\n---\n\n#### ***C++ Code:***\n\nThe best result for the code below is **129ms / 88.9MB** (beats 100% / 100%).\n```c++\nclass Solution {\npublic:\n    int maximumUniqueSubarray(vector<int>& nums) {\n        char nmap[10001]{0};\n        int total = 0, best = 0;\n        for (int left = 0, right = 0; right < nums.size(); right++) {\n            nmap[nums[right]]++, total += nums[right];\n            while (nmap[nums[right]] > 1)\n                nmap[nums[left]]--, total -= nums[left++];\n            best = max(best, total);\n        }\n        return best;\n    }\n};\n```
['C', 'Python', 'Java', 'JavaScript']