Shortest Impossible Sequence of Rolls

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.

Example 1
Inputrolls = [4,2,1,2,3,3,2,4,1], k = 4
Output3
Every sequence of rolls of length 1 and 2 can be taken from rolls, but the sequence [1, 4, 2] cannot, so the shortest impossible length is 3.
Example 2
Inputrolls = [1,1,2,2], k = 2
Output2
Every sequence of rolls of length 1 can be taken from rolls, but the sequence [2, 1] cannot, so the shortest impossible length is 2.

Constraints

  • n == rolls.length
  • 1 <= n <= 10^5
  • 1 <= rolls[i] <= k <= 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate