<p>You are given an integer array <code>rolls</code> of length <code>n</code> and an integer <code>k</code>. You roll a <code>k</code> sided dice numbered from <code>1</code> to <code>k</code>, <code>n</code> times, where the result of the <code>i<sup>th</sup></code> roll is <code>rolls[i]</code>.</p>

<p>Return<em> the length of the <strong>shortest</strong> sequence of rolls so that there&#39;s no such <span data-keyword="subsequence-array">subsequence</span> in </em><code>rolls</code>.</p>

<p>A <strong>sequence of rolls</strong> of length <code>len</code> is the result of rolling a <code>k</code> sided dice <code>len</code> times.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> rolls = [4,2,1,2,3,3,2,4,1], k = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], [3], [4], can be taken from rolls.
Every sequence of rolls of length 2, [1, 1], [1, 2], ..., [4, 4], can be taken from rolls.
The sequence [1, 4, 2] cannot be taken from rolls, so we return 3.
Note that there are other sequences that cannot be taken from rolls.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> rolls = [1,1,2,2], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], can be taken from rolls.
The sequence [2, 1] cannot be taken from rolls, so we return 2.
Note that there are other sequences that cannot be taken from rolls but [2, 1] is the shortest.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> rolls = [1,1,3,2,2,2,3,3], k = 4
<strong>Output:</strong> 1
<strong>Explanation:</strong> The sequence [4] cannot be taken from rolls, so we return 1.
Note that there are other sequences that cannot be taken from rolls but [4] is the shortest.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == rolls.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= rolls[i] &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def shortestSequence(self, rolls: List[int], k: int) -> int:
        # solution redone here, original solution further below
        # we find the minimum index d such that all numbers appear at least once
        # that is sufficient to yield all subsequences of length 1
        # then we reset and find the next minimum index j such that all numbers at least once
        # that is sufficient to yield all subsequences of length 2
        # but was that necessary? or could all subsequences of length 2 have appeared earlier?
        # it was necessary, because the subsequence [rolls[d], rolls[j]] cannot have appeared yet
        # just repeat this, and the same argument holds for subsequences of length m > 2.
        res = 1
        seen = set()
        for x in rolls:
            seen.add(x)
            if len(seen) == k:
                res += 1
                seen = set()
        return res












































        # """
        # For any sequence a1 a2 ... am of length m to appear, we must have that each of
        # the numbers {1, 2 ... k} must appear repetitively m times. Otherwise,
        # we can construct a sequence of length m that does not appear in the array.
        # Find the largest p < m such that the numbers {1, 2 ... k} appear repetitively
        # for p times, but not m times. Then there is some number x such that in the
        # remainder of the array, x never appears again.

        # Now consider the sequence whose first element is the last occurrence
        # of a number from {1, 2 ... k} the first time the set shows up.
        # Its second element is the last occurrence of a number from
        # {1, 2 ... k} the second time the set shows up. And its last element is
        # x. This sequence is length p + 1 <= m, and it does not appear in the array.

        # This is all basically copied from lee215's brilliant insight at
        # https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/solutions/  2322280/java-c-python-one-pass-o-k-space/
        # """
        # res = 0
        # seen = set()
        # for num in rolls:
        #     seen.add(num)
        #     if len(seen) == k:
        #         seen = set()
        #         res += 1
        # return res + 1
713