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 ith
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:
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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The main idea is to try out all possible sequences of dice rolls, starting with the smallest ones. We check each sequence to see if it can be formed from the given rolls, and we stop as soon as we find one that can't.
Here's how the algorithm would work step-by-step:
def find_shortest_impossible_sequence(rolls, k_value):
sequence_length = 1
while True:
# Generate all possible sequences of the current length.
possible_sequences = generate_sequences(sequence_length, k_value)
for sequence in possible_sequences:
if not is_subsequence(sequence, rolls):
# If sequence not found, its the answer.
return sequence
sequence_length += 1
def generate_sequences(sequence_length, k_value):
if sequence_length == 0:
return [[]]
smaller_sequences = generate_sequences(sequence_length - 1, k_value)
sequences = []
for i in range(1, k_value + 1):
for sequence in smaller_sequences:
sequences.append(sequence + [i])
return sequences
def is_subsequence(subsequence, rolls):
subsequence_index = 0
rolls_index = 0
while subsequence_index < len(subsequence) and rolls_index < len(rolls):
if subsequence[subsequence_index] == rolls[rolls_index]:
subsequence_index += 1
rolls_index += 1
# Check if the subsequence was fully matched.
return subsequence_index == len(subsequence)
The goal is to find the shortest sequence of dice rolls that you *cannot* create from a given set of rolls. The fastest way involves checking which numbers are present and building the smallest missing sequence from there.
Here's how the algorithm would work step-by-step:
def shortest_impossible_sequence(rolls, k):
roll_counts = [0] * (k + 1)
for roll in rolls:
roll_counts[roll] += 1
sequence_length = 1
while True:
start_sequence = [1] * sequence_length
end_sequence = [k] * sequence_length
current_sequence = start_sequence.copy()
sequence_number = 0
while True:
temp_counts = roll_counts.copy()
possible = True
# Check if we can form the current sequence.
for number in current_sequence:
if temp_counts[number] > 0:
temp_counts[number] -= 1
else:
possible = False
break
if not possible:
return sequence_length
if current_sequence == end_sequence:
break
# Generate the next sequence.
index = sequence_length - 1
while index >= 0 and current_sequence[index] == k:
current_sequence[index] = 1
index -= 1
if index >= 0:
current_sequence[index] += 1
else:
break
#If all sequences of current length are possible,
#increase the sequence length and try again.
sequence_length += 1
Case | How to Handle |
---|---|
rolls is null or empty | Return 1 immediately, as any sequence of length 1 is impossible. |
k is zero or negative | Return 1 immediately, as a zero or negative k is invalid, leading to an impossible sequence of length 1. |
rolls contains only one distinct value, and that value is 'k' | If all rolls are equal to k, the shortest impossible sequence is of length 2 because we can only make 'k' and thus cannot complete the sequence 1 to k. |
k is 1 | Return 2 if the rolls contains no 1 otherwise return 1, the next missing integer. |
rolls contains very large numbers or is extremely long | The size of rolls and the actual roll values themselves should not cause errors as we only care about the range of 1 to k. |
rolls contains numbers outside the range [1, k] | Ignore values outside the range of [1, k] as they don't contribute to forming the sequence from 1 to k. |
k is a very large number | The algorithm's memory usage depends on the range 'k', so a very large k might cause memory issues and the solution must use memory efficiently. |
rolls does not contain all numbers between 1 and k | The solution should find the shortest sequence that's not present, even if not all numbers from 1 to k are in rolls. |