Given an array of integers cards, return the minimum number of consecutive cards you have to pick up to have a pair of matching cards. If it is impossible, return -1.
Use a HashMap to track the last seen index of each card value. For each card, if seen before, the window size is current_index - last_index + 1. Track the minimum.
- Maintain lastSeen HashMap.
- For each index i: if lastSeen contains cards[i], compute window = i - lastSeen[cards[i]] + 1, update min.
- Update lastSeen[cards[i]] = i.
- Return min or -1 if no pair found.
- Time Complexity: O(N)
- Space Complexity: O(N)