149. Minimum Window Subarray with All Distinct Elements

HardArrayArray

Given an integer array, return the length of the shortest contiguous subarray that contains every distinct value present in the whole array. Return 0 if the array is empty. Input: an integer array.

Input: An integer array.

Output: Integer — shortest qualifying subarray length.

Examples

Example 1
Input: [1,2,2,3,1]
Output: 4
Explanation: Subarray [2,3,1] is too short on distinct... shortest containing {1,2,3} is [1,2,2,3] length 4? Actually [2,3,1] (indices 2-4) contains 2,3,1 = all 3 distinct, length 3.
Example 2
Input: [1,1,1]
Output: 1
Explanation: Only one distinct value.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →