2260. Minimum Consecutive Cards to Pick Up
Medium
You are given an integer array cards
where cards[i]
represents the value of the ith
card. A pair of cards are matching if the cards have the same value.
Return the minimum number of consecutive cards you have to pick up to have a pair of matching cards among the picked cards. If it is impossible to have matching cards, return -1
.
Example 1:
Example 2:
Constraints:
1 <= cards.length <= 105
0 <= cards[i] <= 106
解題
因為他要求的是最小,所以我們可以建立一個map來記錄目前的值出現的index,當下次這個值又出現時,比較和之前出現的index間的距離是否是目前最小距離。
Runtime: 174 ms, faster than 87.18%
Memory Usage: 11.5 MB, less than 92.31%
Previous2259. Remove Digit From Number to Maximize ResultNext2264. Largest 3-Same-Digit Number in String
Last updated