2260. Minimum Consecutive Cards to Pick Up
Question
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
.
Solution
动态规划的思想。
found[]储存每个数字的上一个index
遍历,当数字相同时从found里获得上一个index,计算当前index与last index的距离。
并保存最小值。
Code
1 | class Solution { |
2260. Minimum Consecutive Cards to Pick Up
https://xuanhe95.github.io/2023/05/11/2260-Minimum-Consecutive-Cards-to-Pick-Up/