Shortest Impossible Sequence of Rolls LeetCode Solution

Problem – Shortest Impossible Sequence of Rolls LeetCode Solution

You are given an integer array rolls of length n and an integer k. You roll a k sided dice numbered from 1 to kn times, where the result of the ith roll is rolls[i].

Return the length of the shortest sequence of rolls that cannot be taken from rolls.

sequence of rolls of length len is the result of rolling a k sided dice len times.

Note that the sequence taken does not have to be consecutive as long as it is in order.

Example 1:

Input: rolls = [4,2,1,2,3,3,2,4,1], k = 4
Output: 3
Explanation: 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.

Example 2:

Input: rolls = [1,1,2,2], k = 2
Output: 2
Explanation: 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.

Example 3:

Input: rolls = [1,1,3,2,2,2,3,3], k = 4
Output: 1
Explanation: 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.

Constraints:

  • n == rolls.length
  • 1 <= n <= 105
  • 1 <= rolls[i] <= k <= 105

Shortest Impossible Sequence of Rolls LeetCode Solution in Java

    public int shortestSequence(int[] A, int k) {
        int res = 1;
        Set<Integer> s = new HashSet<>();
        for (int a : A) {
            s.add(a);
            if (s.size() == k) {
                res++;
                s.clear();
            }
        }
        return res;
    }

Shortest Impossible Sequence of Rolls LeetCode Solution in C++

    int shortestSequence(vector<int>& A, int k) {
        int res = 1;
        unordered_set<int> s;
        for (int& a : A) {
            s.insert(a);
            if (s.size() == k) {
                res++;
                s.clear();
            }
        }
        return res;
    }

Shortest Impossible Sequence of Rolls LeetCode Solution in Python

    def shortestSequence(self, rolls, k):
        res = 1
        s = set()
        for a in rolls:
            s.add(a)
            if len(s) == k:
                res += 1
                s.clear()
        return res
Shortest Impossible Sequence of Rolls LeetCode Solution Review:

In our experience, we suggest you solve this Shortest Impossible Sequence of Rolls LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Shortest Impossible Sequence of Rolls LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Shortest Impossible Sequence of Rolls LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.