LeetCode 题解工作台

求出所有子序列的能量和

给你一个长度为 n 的整数数组 nums 和一个 正 整数 k 。 一个整数数组的 能量 定义为和 等于 k 的子序列的数目。 请你返回 nums 中所有子序列的 能量和 。 由于答案可能很大,请你将它对 10 9 + 7 取余 后返回。 示例 1: 输入: nums = [1,2,3], k = …

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

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

bolt

答案摘要

题目需要我们在给定数组 中找到所有子序列 ,然后计算每个 的每个子序列 的和等于 的方案数。 我们定义 表示前 个数构成的若干个子序列中,每个子序列的子序列和等于 的方案数。初始时 $f[0][0] = 1$,其余位置均为 。

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个长度为 n 的整数数组 nums 和一个  整数 k 。

一个整数数组的 能量 定义为和 等于 k 的子序列的数目。

请你返回 nums 中所有子序列的 能量和 。

由于答案可能很大,请你将它对 109 + 7 取余 后返回。

 

示例 1:

输入: nums = [1,2,3], k = 3

输出: 6

解释:

总共有 5 个能量不为 0 的子序列:

  • 子序列 [1,2,3] 有 2 个和为 3 的子序列:[1,2,3][1,2,3] 。
  • 子序列 [1,2,3] 有 1 个和为 3 的子序列:[1,2,3] 。
  • 子序列 [1,2,3] 有 1 个和为 3 的子序列:[1,2,3] 。
  • 子序列 [1,2,3] 有 1 个和为 3 的子序列:[1,2,3] 。
  • 子序列 [1,2,3] 有 1 个和为 3 的子序列:[1,2,3] 。

所以答案为 2 + 1 + 1 + 1 + 1 = 6 。

示例 2:

输入: nums = [2,3,3], k = 5

输出: 4

解释:

总共有 3 个能量不为 0 的子序列:

  • 子序列 [2,3,3] 有 2 个子序列和为 5 :[2,3,3] 和 [2,3,3] 。
  • 子序列 [2,3,3] 有 1 个子序列和为 5 :[2,3,3] 。
  • 子序列 [2,3,3] 有 1 个子序列和为 5 :[2,3,3] 。

所以答案为 2 + 1 + 1 = 4 。

示例 3:

输入: nums = [1,2,3], k = 7

输出: 0

解释:不存在和为 7 的子序列,所以 nums 的能量和为 0 。

 

提示:

  • 1 <= n <= 100
  • 1 <= nums[i] <= 104
  • 1 <= k <= 100
lightbulb

解题思路

方法一:动态规划

题目需要我们在给定数组 nums\textit{nums} 中找到所有子序列 S\textit{S},然后计算每个 S\textit{S} 的每个子序列 T\textit{T} 的和等于 k\textit{k} 的方案数。

我们定义 f[i][j]f[i][j] 表示前 ii 个数构成的若干个子序列中,每个子序列的子序列和等于 jj 的方案数。初始时 f[0][0]=1f[0][0] = 1,其余位置均为 00

对于第 ii 个数 xx,有以下三种情况:

  1. 不在子序列 S\textit{S} 中,此时 f[i][j]=f[i1][j]f[i][j] = f[i-1][j]
  2. 在子序列 S\textit{S},但不在子序列 T\textit{T} 中,此时 f[i][j]=f[i1][j]f[i][j] = f[i-1][j]
  3. 在子序列 S\textit{S},且在子序列 T\textit{T} 中,此时 f[i][j]=f[i1][jx]f[i][j] = f[i-1][j-x]

综上,状态转移方程为:

f[i][j]=f[i1][j]×2+f[i1][jx]f[i][j] = f[i-1][j] \times 2 + f[i-1][j-x]

最终答案为 f[n][k]f[n][k]

时间复杂度 O(n×k)O(n \times k),空间复杂度 O(n×k)O(n \times k)。其中 nn 为数组 nums\textit{nums} 的长度,而 kk 为给定的正整数。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def sumOfPower(self, nums: List[int], k: int) -> int:
        mod = 10**9 + 7
        n = len(nums)
        f = [[0] * (k + 1) for _ in range(n + 1)]
        f[0][0] = 1
        for i, x in enumerate(nums, 1):
            for j in range(k + 1):
                f[i][j] = f[i - 1][j] * 2 % mod
                if j >= x:
                    f[i][j] = (f[i][j] + f[i - 1][j - x]) % mod
        return f[n][k]
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The problem tests knowledge of dynamic programming and subset sum problems.

  • question_mark

    Candidates should demonstrate understanding of state transitions in dynamic programming.

  • question_mark

    Pay attention to how the solution scales with n and k, as efficiency is key for larger inputs.

warning

常见陷阱

外企场景
  • error

    Misunderstanding the contribution of subsequences to the power sum.

  • error

    Failing to update the DP table correctly during iteration.

  • error

    Not handling edge cases such as when no subsequence sums to k, resulting in zero power.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider modifying the problem by changing the target sum k or adding constraints like limiting subsequence length.

  • arrow_right_alt

    What if the array contains duplicates or the integers in nums have different ranges?

  • arrow_right_alt

    Extend the problem to handle multiple target sums, requiring the calculation of power for each.

help

常见问题

外企场景

求出所有子序列的能量和题解:状态·转移·动态规划 | LeetCode #3082 困难