[Java] Sliding window. O(n) Time. O(1) Space. Clean code with comments and dry run
Approach\nUse a sliding window, and keep moving left pointer to get rid of duplicate elements.\nHave a variable to store maxScore.\n\n// Time complexity: O(n) l
**Approach**\nUse a sliding window, and keep moving left pointer to get rid of duplicate elements.\nHave a variable to store maxScore.\n```\n// Time complexity: O(n) linear\n// Space complexity: O(1) constant - actually O(100_001)\nclass Solution {\n    public int maximumUniqueSubarray(int[] nums) {\n        int n = nums.length;\n        int[] seen = new int[100_001]; \n        \n        int left = 0, right = 0;\n        int maxScore = 0, currScore = 0;\n        \n        while (right < n) {\n            seen[nums[right]]++;\n            currScore += nums[right];\n            // Keep moving left pointer until we get rid of duplicate element\n            while (right < n && seen[nums[right]] > 1) {\n                seen[nums[left]]--;\n                currScore -= nums[left];\n                left++;\n            }\n            // Save best score\n            maxScore = Math.max(maxScore, currScore);\n            right++;\n        }\n        return maxScore;\n    }\n}\n```\n\n**Dry run**\n\n```\n\tnums: [4,2,4,5,6]\n    Initially \n    left = 0, right = 0, currScore = 0, maxScore = 0\n    \n    left = 0, right = 1, currScore = 4, maxScore = 4\n    \n    left = 0, right = 2, currScore = 6, maxScore = 6\n    \n    left = 1, right = 3, currScore = 6, maxScore = 6\n    \n    left = 1, right = 4, currScore = 11, maxScore = 11\n    \n    left = 1, right = 5, currScore = 17, maxScore = 17\n\treturn 17\n```
['Sliding Window']