Advertisement

Number of Distinct Roll Sequences - LeetCode 2318 Solution

Number of Distinct Roll Sequences - Complete Solution Guide

Number of Distinct Roll Sequences is LeetCode problem 2318, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer n . You roll a fair 6-sided dice n times. Determine the total number of distinct sequences of rolls possible such that the following conditions are satisfied: The greatest common divisor of any adjacent values in the sequence is equal to 1 . There is at least a gap of 2 rolls between equal valued rolls. More formally, if the value of the i th roll is equal to the value of the j th roll, then abs(i - j) > 2 . Return the total number of distinct sequences possible . Since

Detailed Explanation

The problem asks us to find the number of distinct sequences of rolling a 6-sided die 'n' times, subject to two constraints: 1) The greatest common divisor (GCD) of any two adjacent rolls must be 1, meaning they are coprime. 2) Equal values must be separated by at least two rolls. The input is an integer 'n', and the output is the number of valid sequences modulo 10^9 + 7.

Solution Approach

The solution uses dynamic programming with a 2D array `dp[i][j]` to store the number of valid sequences of length 'k' ending with the sequence `(i+1, j+1)`, where 'i' and 'j' are die face indices (0 to 5). The solution iterates from k=3 to n. For each length k, it calculates a new `dp` array based on the previous `dp` array, ensuring the two constraints are met: GCD of adjacent rolls is 1, and there's a separation of at least 2 rolls between equal values.

Step-by-Step Algorithm

  1. Step 1: Initialize `gcd_ok` array: Create a boolean 2D array to store whether the GCD of two die faces (i+1, j+1) is 1. Pre-compute this for all possible pairs of die faces.
  2. Step 2: Initialize base case `dp` array: Create a 2D array `dp[i][j]` and set `dp[i][j] = 1` if the GCD of `i+1` and `j+1` is 1 and `i != j`. This represents the number of valid sequences of length 2.
  3. Step 3: Iterate from 3 to n: For each 'k' from 3 to 'n', create a `new_dp` array to store the number of valid sequences of length 'k'.
  4. Step 4: Calculate sums for efficient computation: For each possible last roll `p2`, compute the sum of all valid sequences ending with `p2` in the previous iteration. This sum represents the total number of valid sequences of length k-1 that can have 'p2' as the (k-1)-th roll.
  5. Step 5: Update `new_dp` based on previous `dp`: For each possible second-to-last roll `p2` and last roll `p3`, if `p2 != p3` and the GCD of `p2+1` and `p3+1` is 1, then the number of valid sequences of length 'k' ending with `(p2+1, p3+1)` is the sum of all valid sequences of length `k-1` ending with `p2` minus the number of sequences that end with `(p3+1, p2+1)`, preventing a sequence like `..., x, y, x` where x and y are adjacent and identical after reduction.
  6. Step 6: Update `dp` array: Copy the values from `new_dp` to `dp` for the next iteration.
  7. Step 7: Calculate total count: After iterating through all lengths up to 'n', sum all the values in the final `dp` array to get the total number of distinct sequences.
  8. Step 8: Return result modulo 10^9 + 7: Return the total count modulo 10^9 + 7.

Key Insights

  • Insight 1: Dynamic programming is a natural fit because the number of valid sequences for 'n' rolls depends on the valid sequences of 'n-1' and 'n-2' rolls. We need to keep track of the last two rolls to enforce both GCD and separation constraints.
  • Insight 2: We can represent the state of the DP with two variables: the value of the (i-1)th roll and the value of the ith roll.
  • Insight 3: Pre-computing the GCD relationships between die faces avoids repeated calculations and simplifies the logic.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Dynamic Programming, Memoization.

Companies

Asked at: ServiceNow.