LeetCode 2338. Count the Number of Ideal Arrays Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
2338. Count the Number of Ideal Arrays

Description

You are given two integers n and maxValue, which are used to describe an ideal array.

A 0-indexed integer array arr of length n is considered ideal if the following conditions hold:

  • Every arr[i] is a value from 1 to maxValue, for 0 <= i < n.
  • Every arr[i] is divisible by arr[i - 1], for 0 < i < n.

Return the number of distinct ideal arrays of length n. Since the answer may be very large, return it modulo 109 + 7.

 

Example 1:

Input: n = 2, maxValue = 5
Output: 10
Explanation: The following are the possible ideal arrays:
- Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]
- Arrays starting with the value 2 (2 arrays): [2,2], [2,4]
- Arrays starting with the value 3 (1 array): [3,3]
- Arrays starting with the value 4 (1 array): [4,4]
- Arrays starting with the value 5 (1 array): [5,5]
There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.

Example 2:

Input: n = 5, maxValue = 3
Output: 11
Explanation: The following are the possible ideal arrays:
- Arrays starting with the value 1 (9 arrays): 
   - With no other distinct values (1 array): [1,1,1,1,1] 
   - With 2nd distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]
   - With 2nd distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]
- Arrays starting with the value 2 (1 array): [2,2,2,2,2]
- Arrays starting with the value 3 (1 array): [3,3,3,3,3]
There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.

 

Constraints:

  • 2 <= n <= 104
  • 1 <= maxValue <= 104

Solutions

Solution 1: Dynamic Programming

Let f[i][j] represent the number of sequences ending with i and consisting of j distinct elements. The initial value is f[i][1] = 1.

Consider n balls, which are eventually divided into j parts. Using the "separator method," we can insert j-1 separators into the n-1 positions, and the number of combinations is cn-1^{j-1}.

We can preprocess the combination numbers c[i][j] using the recurrence relation c[i][j] = c[i-1][j] + c[i-1][j-1]. Specifically, when j=0, c[i][j] = 1.

The final answer is:

∑\limitsi=1^{k}∑\limitsj=1^{log_2 k + 1} f[i][j] × cn-1^{j-1}

where k represents the maximum value of the array, i.e., maxValue.

  • Time Complexity: O(m × log^2 m)
  • Space Complexity: O(m × log m)
PythonJavaC++Go
class Solution: def idealArrays(self, n: int, maxValue: int) -> int: @cache def dfs(i, cnt): res = c[-1][cnt - 1] if cnt < n: k = 2 while k * i <= maxValue: res = (res + dfs(k * i, cnt + 1)) % mod k += 1 return res c = [[0] * 16 for _ in range(n)] mod = 10**9 + 7 for i in range(n): for j in range(min(16, i + 1)): c[i][j] = 1 if j == 0 else (c[i - 1][j] + c[i - 1][j - 1]) % mod ans = 0 for i in range(1, maxValue + 1): ans = (ans + dfs(i, 1)) % mod return ans(code-box)

Solution 2

PythonJavaC++GoTypeScript
class Solution: def idealArrays(self, n: int, maxValue: int) -> int: c = [[0] * 16 for _ in range(n)] mod = 10**9 + 7 for i in range(n): for j in range(min(16, i + 1)): c[i][j] = 1 if j == 0 else (c[i - 1][j] + c[i - 1][j - 1]) % mod f = [[0] * 16 for _ in range(maxValue + 1)] for i in range(1, maxValue + 1): f[i][1] = 1 for j in range(1, 15): for i in range(1, maxValue + 1): k = 2 while k * i <= maxValue: f[k * i][j + 1] = (f[k * i][j + 1] + f[i][j]) % mod k += 1 ans = 0 for i in range(1, maxValue + 1): for j in range(1, 16): ans = (ans + f[i][j] * c[-1][j - 1]) % mod return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !