Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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:
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.
Constraints:
1 <= cards.length <= 105
0 <= cards[i] <= 106
int minimumCardPickup(vector<int>& cards) {
int last[1000001] = {}, res = INT_MAX;
for (int i = 0; i < cards.size(); ++i) {
if (last[cards[i]])
res = min(res, i - last[cards[i]] + 2);
last[cards[i]] = i + 1;
}
return res == INT_MAX ? -1 : res;
}
class Solution
{
public int minimumCardPickup(int[] cards)
{
Map<Integer,Integer> map = new HashMap<>();
int min = Integer.MAX_VALUE;
for(int i = 0; i < cards.length; i++)
{
if(map.containsKey(cards[i]))
min = Math.min(i-map.get(cards[i])+1,min); // Check if the difference in indices is smaller than minimum
map.put(cards[i],i); // Update the last found index of the card
}
return min == Integer.MAX_VALUE?-1:min; // Repetition found or not
}
}
class Solution:
def minimumCardPickup(self, cards: List[int]) -> int:
minPick = float('inf')
seen = {}
for i, n in enumerate(cards):
if n in seen:
if i - seen[n] + 1 < minPick:
minPick = i - seen[n] + 1
seen[n] = i
if minPick == float('inf'):
return -1
return minPick
In our experience, we suggest you solve this Minimum Consecutive Cards to Pick Up LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Consecutive Cards to Pick Up LeetCode Solution
I hope this Minimum Consecutive Cards to Pick Up LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>