A turnstile records the badge id of every entry in badges. Security flags a badge if it is scanned twice within k entries of itself, i.e. there are indices i < j with badges[i] == badges[j] and j - i <= k. Return True if any badge is flagged, otherwise False.
Examples
Input: badges = [10, 20, 30, 40, 20], k = 3
Output: True
Explanation: badge 20 appears at indices 1 and 4, and 4 - 1 <= 3.
Input: badges = [10, 20, 30, 40, 20], k = 2
Output: False
Explanation: the two scans of badge 20 are 3 entries apart, more than 2.
Input: badges = [7, 3, 9, 7, 3], k = 2
Output: False
Constraints
0 <= len(badges) <= 10**50 <= k <= 10**5-10**9 <= badges[i] <= 10**9- Target complexity: O(n) time; comparing every pair within distance k is too slow for the largest tests.
Goals
- Maintain a set of the last k values as the window moves
- Return early as soon as a repeat is detected