539. Minimum Window Containing All Required Elements

HardBinary SearchSliding WindowHash

Given an array and a list of required values, return the length of the smallest contiguous window of the array that contains every required value at least once (duplicates in the required list mean that many occurrences are needed), or -1 if no such window exists. The input is JSON {arr, required}. Return the minimum window length.

Input: JSON {arr, required}.

Output: Integer — the minimum window length, or -1.

Examples

Example 1
Input: {"arr":[1,2,1,3,2],"required":[1,2,3]}
Output: 3
Explanation: Window [1,3,2] contains all required.
Example 2
Input: {"arr":[1,1,1],"required":[1]}
Output: 1
Explanation: A single 1 suffices.
Example 3
Input: {"arr":[1,2,3],"required":[4]}
Output: -1
Explanation: 4 never appears.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →