Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1050 <= cards[i] <= 106Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Sliding Window
[3,4,2,3,4,7]
[1,0,5,3]
longest-substring-without-repeating-characters)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2260: Minimum Consecutive Cards to Pick Up
class Solution {
public int minimumCardPickup(int[] cards) {
Map<Integer, Integer> last = new HashMap<>();
int n = cards.length;
int ans = n + 1;
for (int i = 0; i < n; ++i) {
if (last.containsKey(cards[i])) {
ans = Math.min(ans, i - last.get(cards[i]) + 1);
}
last.put(cards[i], i);
}
return ans > n ? -1 : ans;
}
}
// Accepted solution for LeetCode #2260: Minimum Consecutive Cards to Pick Up
func minimumCardPickup(cards []int) int {
last := map[int]int{}
n := len(cards)
ans := n + 1
for i, x := range cards {
if j, ok := last[x]; ok && ans > i-j+1 {
ans = i - j + 1
}
last[x] = i
}
if ans > n {
return -1
}
return ans
}
# Accepted solution for LeetCode #2260: Minimum Consecutive Cards to Pick Up
class Solution:
def minimumCardPickup(self, cards: List[int]) -> int:
last = {}
ans = inf
for i, x in enumerate(cards):
if x in last:
ans = min(ans, i - last[x] + 1)
last[x] = i
return -1 if ans == inf else ans
// Accepted solution for LeetCode #2260: Minimum Consecutive Cards to Pick Up
/**
* [2260] Minimum Consecutive Cards to Pick Up
*
* You are given an integer array cards where cards[i] represents the value of the i^th 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 <= 10^5
* 0 <= cards[i] <= 10^6
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/
// discuss: https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn minimum_card_pickup(cards: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2260_example_1() {
let cards = vec![3, 4, 2, 3, 4, 7];
let result = 4;
assert_eq!(Solution::minimum_card_pickup(cards), result);
}
#[test]
#[ignore]
fn test_2260_example_2() {
let cards = vec![1, 0, 5, 3];
let result = -1;
assert_eq!(Solution::minimum_card_pickup(cards), result);
}
}
// Accepted solution for LeetCode #2260: Minimum Consecutive Cards to Pick Up
function minimumCardPickup(cards: number[]): number {
const n = cards.length;
const last = new Map<number, number>();
let ans = n + 1;
for (let i = 0; i < n; ++i) {
if (last.has(cards[i])) {
ans = Math.min(ans, i - last.get(cards[i]) + 1);
}
last.set(cards[i], i);
}
return ans > n ? -1 : ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.