Ctrl K

Shortest Roll Series Never Seen

Hard
A die with faces 1, 2, …, faces was rolled several times in a row, and the results are stored in rolls in the order of the throws. A series is any sequence of face values. A series *occurs* in rolls if it can be obtained from rolls by crossing out some throws (the rest keep their order). Find the length of the shortest series that does not occur in rolls.
Example 1
rolls=[2, 1, 1, 2, 2], faces=2 → 3

Every face came up, and all pairs [1, 1], [1, 2], [2, 1], [2, 2] occur too. The series [1, 2, 1] does not: every 2 that follows a 1 comes at the very end, with no 1 after it.

Example 2
rolls=[3, 1, 3, 3], faces=3 → 1

A 2 never came up, so even [2] cannot be found.

Example 3
rolls=[1, 1, 1], faces=1 → 4

The die has a single face: one, two and three 1s can be found, but not four.

Constraints
  • 1 ≤ len(rolls) ≤ 10⁵
  • 1 ≤ rolls[i] ≤ faces ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNarrowest Range Touching Every List HardFewest Inserts to Reveal a Pattern HardA Shelf With Matching Ends HardPull Matching Cards Apart HardBalancing Two Shelves Cheaply Hard
loading Python…
Input
rolls=[2, 1, 1, 2, 2], faces=2
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.