Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2260.html
2260. Minimum Consecutive Cards to Pick Up
 Difficulty: Medium.
 Related Topics: Array, Hash Table, Sliding Window.
 Similar Questions: Longest Substring Without Repeating Characters.
Problem
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
Solution (Java, C++, Python)

class Solution { public int minimumCardPickup(int[] cards) { int mindiff = Integer.MAX_VALUE; Map<Integer, Integer> map = new HashMap<>(); int n = cards.length; for (int i = 0; i < n; i++) { if (map.containsKey(cards[i])) { int j = map.get(cards[i]); mindiff = Math.min(mindiff, i  j + 1); } map.put(cards[i], i); } if (mindiff == Integer.MAX_VALUE) { return 1; } return mindiff; } }

Todo

class Solution: def minimumCardPickup(self, cards: List[int]) > int: m = {} ans = 10**6 for i, c in enumerate(cards): if c in m: ans = min(ans, i  m[c] + 1) m[c] = i return 1 if ans == 10**6 else ans ############ # 2260. Minimum Consecutive Cards to Pick Up # https://leetcode.com/problems/minimumconsecutivecardstopickup/ class Solution: def minimumCardPickup(self, cards: List[int]) > int: res = float('inf') last = {} for j, x in enumerate(cards): if x in last: res = min(res, j  last[x] + 1) last[x] = j return 1 if res == float('inf') else res
Explain:
nope.
Complexity:
 Time complexity : O(n).
 Space complexity : O(n).