FastPrepShortest Window by Distinct-Value Sum

Shortest Window by Distinct-Value Sum

Oracle logoOracle● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

For a contiguous window in nums, its distinct-value sum adds each different value exactly once. The reported example gives [3,3,2] a sum of 5, because the repeated 3 is counted once.

Return the minimum number of positions in a nonempty contiguous window whose distinct-value sum is at least target. Count every position in its length, including repeated values.

For this exercise, assume all array values and the target are positive integers. Return 0 if no window qualifies, including when the array is empty. Keep the input unchanged.

Function

minimumDistinctSumWindow(nums: int[], target: int) → int

Examples

Example 1

nums = [3,3,2]target = 5return = 2

The last two positions contain 3 and 2, whose distinct-value sum is 5. No singleton reaches 5; the full three-position window also has sum 5 but is longer.

Example 2

nums = [1,1,1]target = 2return = 0

Every nonempty window contains only the distinct value 1, so its distinct-value sum is 1 and cannot reach 2.

Example 3

nums = []target = 1return = 0

There is no nonempty window.

Constraints

  • For this exercise, assume 0 <= nums.length <= 1000.
  • For this exercise, assume 1 <= nums[i] <= 1000000.
  • For this exercise, assume 1 <= target <= 1000000000.

More Oracle problems

See Oracle hiring insights
Saved
public int minimumDistinctSumWindow(int[] nums, int target) {
    // Write your code here.
}
nums[3,3,2]
target5
expected2
Checking account…