Shortest Impossible Sequence of Rolls - Problem
You are given an integer array rolls of length n and an integer k. You roll a k-sided dice numbered from 1 to k, n times, where the result of the i-th roll is rolls[i].
Return the length of the shortest sequence of rolls so that there's no such subsequence in rolls.
A sequence of rolls of length len is the result of rolling a k-sided dice len times.
Input & Output
Example 1 — Basic Case
$
Input:
rolls = [4,2,1,2,3,3,2,4,1], k = 4
›
Output:
2
💡 Note:
We can form any sequence of length 1: [1], [2], [3], [4] all appear in rolls. For length 2, we need all possible pairs, but [1,1] never appears as a subsequence, so answer is 2.
Example 2 — Immediate Missing
$
Input:
rolls = [1,1,2,2], k = 4
›
Output:
1
💡 Note:
Values 3 and 4 never appear in rolls, so sequences [3] and [4] are impossible. The shortest impossible sequence has length 1.
Example 3 — Longer Sequence
$
Input:
rolls = [1,1,1,2,2,2,3,3,3], k = 3
›
Output:
2
💡 Note:
All length 1 sequences appear: [1], [2], [3]. But some length 2 sequences like [1,2] don't appear as subsequences, so answer is 2.
Constraints
- 1 ≤ rolls.length ≤ 105
- 1 ≤ rolls[i], k ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of dice rolls and number of sides k
2
Process
Track which values we've seen at each sequence length
3
Output
Length of shortest impossible sequence
Key Takeaway
🎯 Key Insight: Use greedy approach to track when all dice values have been seen at each sequence length
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code