LeetCode 题解工作台

统计理想数组的数目

给你两个整数 n 和 maxValue ,用于描述一个 理想数组 。 对于下标从 0 开始、长度为 n 的整数数组 arr ,如果满足以下条件,则认为该数组是一个 理想数组 : 每个 arr[i] 都是从 1 到 maxValue 范围内的一个值,其中 0 。 每个 arr[i] 都可以被 arr[…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

设 表示以 结尾,且由 个不同元素构成的序列的方案数。初始值 。 考虑 个小球,最终划分为 份,那么可以用“隔板法”,即在 个位置上插入 个隔板,那么组合数为 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个整数 nmaxValue ,用于描述一个 理想数组

对于下标从 0 开始、长度为 n 的整数数组 arr ,如果满足以下条件,则认为该数组是一个 理想数组

  • 每个 arr[i] 都是从 1maxValue 范围内的一个值,其中 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 <= 104
  • 1 <= maxValue <= 104
lightbulb

解题思路

方法一:动态规划

f[i][j]f[i][j] 表示以 ii 结尾,且由 jj 个不同元素构成的序列的方案数。初始值 f[i][1]=1f[i][1]=1

考虑 nn 个小球,最终划分为 jj 份,那么可以用“隔板法”,即在 n1n-1 个位置上插入 j1j-1 个隔板,那么组合数为 cn1j1c_{n-1}^{j-1}

我们可以预处理组合数 c[i][j]c[i][j],根据递推公式 c[i][j]=c[i1][j]+c[i1][j1]c[i][j]=c[i-1][j]+c[i-1][j-1] 求得,特别地,当 j=0j=0 时,c[i][j]=1c[i][j]=1

最终的答案为

i=1kj=1log2k+1f[i][j]×cn1j1\sum\limits_{i=1}^{k}\sum\limits_{j=1}^{\log_2 k + 1} f[i][j] \times c_{n-1}^{j-1}

其中 kk 表示数组的最大值,即 maxValue\textit{maxValue}

时间复杂度 O(m×log2m)O(m \times \log^2 m),空间复杂度 O(m×logm)O(m \times \log m)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
speed

复杂度分析

指标
时间O((n+\omega(m))\cdot\omega(m)+m\omega(m))
空间O((n+\log(m))\cdot\log(m))
psychology

面试官常问的追问

外企场景
  • 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.

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

统计理想数组的数目题解:状态·转移·动态规划 | LeetCode #2338 困难