Problem · Array
Maximum Sum of a Unique-Element Subarray
Learn this problemProblem statement
Given a non-empty integer array nums containing only non-negative values, return the maximum sum of a contiguous subarray whose elements are all distinct.
Function
maximumUniqueSubarraySum(nums: int[]) → intExamples
Example 1
nums = [4,2,4,5,6]return = 17The subarray [2,4,5,6] has distinct values and sum 17, which is maximal.
Example 2
nums = [5,2,1,2,5,2,1,2,5]return = 8A maximum-sum distinct subarray is [5,2,1], whose sum is 8.
Example 3
nums = [0,0,0]return = 0Any one-element subarray [0] is valid and has sum 0.
Constraints
1 <= nums.length <= 10^50 <= nums[i] <= 10^4- The answer fits in a 32-bit signed integer.