← 返回 walmartlabs 的题目列表Minimum Talent-Complete Team from Every Start
类型:qbank
Given a student talent array whose values range from 1 through talentsCount, return for every starting index the length of the shortest contiguous segment containing every talent; return -1 when no suffix segment from that index can contain them all. The input size reaches 10^5, requiring O(n) time.
Requirements
Input: talentsCount, the number of talent types, and talent, an array in which each student has exactly one talent labeled from 1 through talentsCount.
A team must be a contiguous segment and contain every talent type at least once.
For each possible starting index, return the length of the shortest qualifying segment beginning at that index.
Return -1 at positions from which the remaining suffix cannot cover every talent.
The data size reaches 10^5, so the expected solution is O(n).
Examples
talentsCount = 3
talent = [1, 2, 3, 2, 1]
output = [3, 4, 3, -1, -1]
Starting at index 0, [1, 2, 3] is the shortest complete team. Starting at index 1 requires [2, 3, 2, 1]; index 2 requires [3, 2, 1]. The suffixes beginning at indices 3 and 4 cannot cover all three talents.
Notes
The OA identifies this as a sliding-window / hash-map problem and explicitly calls out the O(n) requirement.