LeetCode 题解工作台
统计理想数组的数目
给你两个整数 n 和 maxValue ,用于描述一个 理想数组 。 对于下标从 0 开始、长度为 n 的整数数组 arr ,如果满足以下条件,则认为该数组是一个 理想数组 : 每个 arr[i] 都是从 1 到 maxValue 范围内的一个值,其中 0 。 每个 arr[i] 都可以被 arr[…
4
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
设 表示以 结尾,且由 个不同元素构成的序列的方案数。初始值 。 考虑 个小球,最终划分为 份,那么可以用“隔板法”,即在 个位置上插入 个隔板,那么组合数为 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你两个整数 n 和 maxValue ,用于描述一个 理想数组 。
对于下标从 0 开始、长度为 n 的整数数组 arr ,如果满足以下条件,则认为该数组是一个 理想数组 :
- 每个
arr[i]都是从1到maxValue范围内的一个值,其中0 <= i < n。 - 每个
arr[i]都可以被arr[i - 1]整除,其中0 < i < n。
返回长度为 n 的 不同 理想数组的数目。由于答案可能很大,返回对 109 + 7 取余的结果。
示例 1:
输入:n = 2, maxValue = 5 输出:10 解释:存在以下理想数组: - 以 1 开头的数组(5 个):[1,1]、[1,2]、[1,3]、[1,4]、[1,5] - 以 2 开头的数组(2 个):[2,2]、[2,4] - 以 3 开头的数组(1 个):[3,3] - 以 4 开头的数组(1 个):[4,4] - 以 5 开头的数组(1 个):[5,5] 共计 5 + 2 + 1 + 1 + 1 = 10 个不同理想数组。
示例 2:
输入:n = 5, maxValue = 3 输出:11 解释:存在以下理想数组: - 以 1 开头的数组(9 个): - 不含其他不同值(1 个):[1,1,1,1,1] - 含一个不同值 2(4 个):[1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2] - 含一个不同值 3(4 个):[1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3] - 以 2 开头的数组(1 个):[2,2,2,2,2] - 以 3 开头的数组(1 个):[3,3,3,3,3] 共计 9 + 1 + 1 = 11 个不同理想数组。
提示:
2 <= n <= 1041 <= maxValue <= 104
解题思路
方法一:动态规划
设 表示以 结尾,且由 个不同元素构成的序列的方案数。初始值 。
考虑 个小球,最终划分为 份,那么可以用“隔板法”,即在 个位置上插入 个隔板,那么组合数为 。
我们可以预处理组合数 ,根据递推公式 求得,特别地,当 时,。
最终的答案为
其中 表示数组的最大值,即 。
时间复杂度 ,空间复杂度 。
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
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | O((n+\omega(m))\cdot\omega(m)+m\omega(m)) |
| 空间 | O((n+\log(m))\cdot\log(m)) |
面试官常问的追问
外企场景- question_mark
Candidates should demonstrate a clear understanding of dynamic programming and state transitions.
- question_mark
Look for efficient handling of large numbers and the use of modulo operations.
- question_mark
Candidates should optimize both time and space complexity while working with dynamic arrays.
常见陷阱
外企场景- error
Failing to correctly handle the modulo operation can lead to incorrect results for large numbers.
- error
Not optimizing the dynamic programming approach can result in time or space inefficiencies.
- error
Overcomplicating the state transition process when the solution can be simplified by recognizing the non-decreasing property of the array.
进阶变体
外企场景- arrow_right_alt
The problem could be modified by changing the allowed range for values in the array, requiring adjustments to the dynamic programming approach.
- arrow_right_alt
Instead of returning the count modulo 10^9 + 7, the problem could ask for the sum of all possible ideal arrays.
- arrow_right_alt
The problem could involve a different type of array structure, such as allowing for strictly increasing arrays or arrays with no repetition.