#2260
Minimum Consecutive Cards to Pick Up
specialist · 815 · lc medium +31 · verified · 53.6% accepted · 1,087 likes · top 45%
Description
You are given an integer array cards where cards[i] is the value on the ith card. Two cards form a matching pair when they have the same value.
Return the minimum number of consecutive cards you must pick up in order to include at least one matching pair. If no matching pair exists, return -1.
Example 1:
Input: cards = [3,4,2,3,4,7]
Output: 4
Explanation: We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3. Note that picking up the cards [4,2,3,4] is also optimal.
Example 2:
Input: cards = [1,0,5,3]
Output: -1
Explanation: There is no way to pick up a set of consecutive cards that contain a pair of matching cards.
Code
1
2
3