|
| 1 | +/* |
| 2 | + You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. |
| 3 | +
|
| 4 | + Return the maximum score you can get by erasing exactly one subarray. |
| 5 | +
|
| 6 | + An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r). |
| 7 | +
|
| 8 | + |
| 9 | +
|
| 10 | + Example 1: |
| 11 | +
|
| 12 | + Input: nums = [4,2,4,5,6] |
| 13 | + Output: 17 |
| 14 | + Explanation: The optimal subarray here is [2,4,5,6]. |
| 15 | + Example 2: |
| 16 | +
|
| 17 | + Input: nums = [5,2,1,2,5,2,1,2,5] |
| 18 | + Output: 8 |
| 19 | + Explanation: The optimal subarray here is [5,2,1] or [1,2,5]. |
| 20 | +
|
| 21 | + Explanation: |
| 22 | +
|
| 23 | + startWindow: The left end of the sliding window. |
| 24 | + windowSum: Sum of elements within the current window. |
| 25 | + res: Result, initialized to 0, representing the maximum unique subarray sum. |
| 26 | + mp: HashMap to store the last index where each element was seen. |
| 27 | + Sliding Window: |
| 28 | +
|
| 29 | + Use a for loop to iterate through the array with the endWindow as the right end of the window. |
| 30 | + Check if the current element is already in the window using a while loop. |
| 31 | + If yes, remove the element at the start of the window from the HashMap and update windowSum and startWindow. |
| 32 | + Update HashMap and windowSum: |
| 33 | +
|
| 34 | + Add the current element to the HashMap and update windowSum. |
| 35 | + Update Result: |
| 36 | +
|
| 37 | + Update the result (res) with the maximum unique subarray sum. |
| 38 | + Return Result: |
| 39 | +
|
| 40 | + Return the final result, which represents the maximum unique subarray sum. |
| 41 | + This code efficiently finds the maximum sum of a subarray where all elements are unique using a sliding window and a HashMap to keep track of the last index of each element encountered. |
| 42 | +
|
| 43 | +*/ |
| 44 | +class Solution { |
| 45 | + public int maximumUniqueSubarray(int[] nums) { |
| 46 | + // Initialize pointers, windowSum, and result |
| 47 | + int startWindow = 0; // The left end of the sliding window |
| 48 | + int windowSum = 0; // Sum of elements within the current window |
| 49 | + int res = 0; // Result, which represents the maximum unique subarray sum |
| 50 | + |
| 51 | + // HashMap to store the last index where each element was seen |
| 52 | + HashMap<Integer, Integer> mp = new HashMap<>(); |
| 53 | + |
| 54 | + // Iterate through the array using a sliding window approach |
| 55 | + for (int endWindow = 0; endWindow < nums.length; endWindow++) { |
| 56 | + // Check if the current element is already in the window |
| 57 | + while (mp.containsKey(nums[endWindow])) { |
| 58 | + // Remove the element at the start of the window from the HashMap and update windowSum |
| 59 | + mp.remove(nums[startWindow]); |
| 60 | + windowSum -= nums[startWindow]; |
| 61 | + startWindow++; |
| 62 | + } |
| 63 | + |
| 64 | + // Add the current element to the HashMap and update windowSum |
| 65 | + mp.put(nums[endWindow], 1); |
| 66 | + windowSum += nums[endWindow]; |
| 67 | + |
| 68 | + // Update the result with the maximum unique subarray sum |
| 69 | + res = Math.max(res, windowSum); |
| 70 | + } |
| 71 | + |
| 72 | + // Return the result, which represents the maximum unique subarray sum |
| 73 | + return res; |
| 74 | + } |
| 75 | +} |
0 commit comments