Imagine you're analyzing dice roll patterns to find the shortest sequence that's guaranteed to never appear in your data!
You have an array rolls of length n representing the results of rolling a k-sided dice (numbered 1 to k) exactly n times. Your task is to find the length of the shortest sequence of consecutive rolls that cannot be found as a subsequence in the given rolls array.
Key insight: A sequence of length len represents the result of rolling the dice len times consecutively. We need to find the minimum len such that there exists at least one sequence of length len that doesn't appear as a subsequence in our rolls.
Example: If rolls = [4,2,1,2,3] and k = 4, we need to find the shortest length where at least one possible sequence of that length is missing from the subsequences of our rolls.
Input & Output
Visualization
Time & Space Complexity
Single pass through the rolls array, with O(1) set operations
Only need to store at most k different values in the set
Constraints
- 1 โค n โค 105
- 1 โค rolls[i] โค k โค 105
- rolls[i] represents the result of rolling a k-sided dice