2260. Minimum Consecutive Cards to Pick Up LeetCode Solution
In this guide, you will get 2260. Minimum Consecutive Cards to Pick Up LeetCode Solution with the best time and space complexity. The solution to Minimum Consecutive Cards to Pick Up problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Consecutive Cards to Pick Up solution in C++
- Minimum Consecutive Cards to Pick Up solution in Java
- Minimum Consecutive Cards to Pick Up solution in Python
- Additional Resources
Problem Statement of Minimum Consecutive Cards to Pick Up
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2260. Minimum Consecutive Cards to Pick Up LeetCode Solution in C++
class Solution {
public:
int minimumCardPickup(vector<int>& cards) {
int ans = INT_MAX;
unordered_map<int, int> lastSeen;
for (int i = 0; i < cards.size(); ++i) {
if (const auto it = lastSeen.find(cards[i]); it != lastSeen.cend())
ans = min(ans, i - it->second + 1);
lastSeen[cards[i]] = i;
}
return ans == INT_MAX ? -1 : ans;
}
};
/* code provided by PROGIEZ */
2260. Minimum Consecutive Cards to Pick Up LeetCode Solution in Java
class Solution {
public int minimumCardPickup(int[] cards) {
int ans = Integer.MAX_VALUE;
Map<Integer, Integer> lastSeen = new HashMap<>();
for (int i = 0; i < cards.length; ++i) {
if (lastSeen.containsKey(cards[i]))
ans = Math.min(ans, i - lastSeen.get(cards[i]) + 1);
lastSeen.put(cards[i], i);
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
}
// code provided by PROGIEZ
2260. Minimum Consecutive Cards to Pick Up LeetCode Solution in Python
class Solution:
def minimumCardPickup(self, cards: list[int]) -> int:
ans = math.inf
lastSeen = {}
for i, card in enumerate(cards):
if card in lastSeen:
ans = min(ans, i - lastSeen[card] + 1)
lastSeen[card] = i
return -1 if ans == math.inf else ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.