Skip to content

Commit 2d4fe0d

Browse files
authored
feat: add solutions to lc problem: No.3082 (doocs#3424)
No.3082.Find the Sum of the Power of All Subsequences
1 parent 62f9c29 commit 2d4fe0d

File tree

12 files changed

+561
-6
lines changed

12 files changed

+561
-6
lines changed

solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README.md

+208-3
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,237 @@ tags:
9797

9898
<!-- solution:start -->
9999

100-
### 方法一
100+
### 方法一:动态规划
101+
102+
题目需要我们在给定数组 $\textit{nums}$ 中找到所有子序列 $\textit{S}$,然后计算每个 $\textit{S}$ 的每个子序列 $\textit{T}$ 的和等于 $\textit{k}$ 的方案数。
103+
104+
我们定义 $f[i][j]$ 表示前 $i$ 个数构成的若干个子序列中,每个子序列的子序列和等于 $j$ 的方案数。初始时 $f[0][0] = 1$,其余位置均为 $0$。
105+
106+
对于第 $i$ 个数 $x$,有以下三种情况:
107+
108+
1. 不在子序列 $\textit{S}$ 中,此时 $f[i][j] = f[i-1][j]$;
109+
1. 在子序列 $\textit{S}$,但不在子序列 $\textit{T}$ 中,此时 $f[i][j] = f[i-1][j]$;
110+
1. 在子序列 $\textit{S}$,且在子序列 $\textit{T}$ 中,此时 $f[i][j] = f[i-1][j-x]$。
111+
112+
综上,状态转移方程为:
113+
114+
$$
115+
f[i][j] = f[i-1][j] \times 2 + f[i-1][j-x]
116+
$$
117+
118+
最终答案为 $f[n][k]$。
119+
120+
时间复杂度 $O(n \times k)$,空间复杂度 $O(n \times k)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $k$ 为给定的正整数。
101121

102122
<!-- tabs:start -->
103123

104124
#### Python3
105125

106126
```python
107-
127+
class Solution:
128+
def sumOfPower(self, nums: List[int], k: int) -> int:
129+
mod = 10**9 + 7
130+
n = len(nums)
131+
f = [[0] * (k + 1) for _ in range(n + 1)]
132+
f[0][0] = 1
133+
for i, x in enumerate(nums, 1):
134+
for j in range(k + 1):
135+
f[i][j] = f[i - 1][j] * 2 % mod
136+
if j >= x:
137+
f[i][j] = (f[i][j] + f[i - 1][j - x]) % mod
138+
return f[n][k]
108139
```
109140

110141
#### Java
111142

112143
```java
113-
144+
class Solution {
145+
public int sumOfPower(int[] nums, int k) {
146+
final int mod = (int) 1e9 + 7;
147+
int n = nums.length;
148+
int[][] f = new int[n + 1][k + 1];
149+
f[0][0] = 1;
150+
for (int i = 1; i <= n; ++i) {
151+
for (int j = 0; j <= k; ++j) {
152+
f[i][j] = (f[i - 1][j] * 2) % mod;
153+
if (j >= nums[i - 1]) {
154+
f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod;
155+
}
156+
}
157+
}
158+
return f[n][k];
159+
}
160+
}
114161
```
115162

116163
#### C++
117164

118165
```cpp
166+
class Solution {
167+
public:
168+
int sumOfPower(vector<int>& nums, int k) {
169+
const int mod = 1e9 + 7;
170+
int n = nums.size();
171+
int f[n + 1][k + 1];
172+
memset(f, 0, sizeof(f));
173+
f[0][0] = 1;
174+
for (int i = 1; i <= n; ++i) {
175+
for (int j = 0; j <= k; ++j) {
176+
f[i][j] = (f[i - 1][j] * 2) % mod;
177+
if (j >= nums[i - 1]) {
178+
f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod;
179+
}
180+
}
181+
}
182+
return f[n][k];
183+
}
184+
};
185+
```
119186
187+
#### Go
188+
189+
```go
190+
func sumOfPower(nums []int, k int) int {
191+
const mod int = 1e9 + 7
192+
n := len(nums)
193+
f := make([][]int, n+1)
194+
for i := range f {
195+
f[i] = make([]int, k+1)
196+
}
197+
f[0][0] = 1
198+
for i := 1; i <= n; i++ {
199+
for j := 0; j <= k; j++ {
200+
f[i][j] = (f[i-1][j] * 2) % mod
201+
if j >= nums[i-1] {
202+
f[i][j] = (f[i][j] + f[i-1][j-nums[i-1]]) % mod
203+
}
204+
}
205+
}
206+
return f[n][k]
207+
}
208+
```
209+
210+
#### TypeScript
211+
212+
```ts
213+
function sumOfPower(nums: number[], k: number): number {
214+
const mod = 10 ** 9 + 7;
215+
const n = nums.length;
216+
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
217+
f[0][0] = 1;
218+
for (let i = 1; i <= n; ++i) {
219+
for (let j = 0; j <= k; ++j) {
220+
f[i][j] = (f[i - 1][j] * 2) % mod;
221+
if (j >= nums[i - 1]) {
222+
f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod;
223+
}
224+
}
225+
}
226+
return f[n][k];
227+
}
228+
```
229+
230+
<!-- tabs:end -->
231+
232+
<!-- solution:end -->
233+
234+
<!-- solution:start -->
235+
236+
### 方法二:动态规划(优化)
237+
238+
方法一中的状态转移方程中,$f[i][j]$ 的值只与 $f[i-1][j]$ 和 $f[i-1][j-x]$ 有关,因此我们可以优化第一维空间,从而将空间复杂度优化为 $O(k)$。
239+
240+
时间复杂度 $O(n \times k)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $k$ 为给定的正整数。
241+
242+
<!-- tabs:start -->
243+
244+
#### Python3
245+
246+
```python
247+
class Solution:
248+
def sumOfPower(self, nums: List[int], k: int) -> int:
249+
mod = 10**9 + 7
250+
f = [1] + [0] * k
251+
for x in nums:
252+
for j in range(k, -1, -1):
253+
f[j] = (f[j] * 2 + (0 if j < x else f[j - x])) % mod
254+
return f[k]
255+
```
256+
257+
#### Java
258+
259+
```java
260+
class Solution {
261+
public int sumOfPower(int[] nums, int k) {
262+
final int mod = (int) 1e9 + 7;
263+
int[] f = new int[k + 1];
264+
f[0] = 1;
265+
for (int x : nums) {
266+
for (int j = k; j >= 0; --j) {
267+
f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod;
268+
}
269+
}
270+
return f[k];
271+
}
272+
}
273+
```
274+
275+
#### C++
276+
277+
```cpp
278+
class Solution {
279+
public:
280+
int sumOfPower(vector<int>& nums, int k) {
281+
const int mod = 1e9 + 7;
282+
int f[k + 1];
283+
memset(f, 0, sizeof(f));
284+
f[0] = 1;
285+
for (int x : nums) {
286+
for (int j = k; j >= 0; --j) {
287+
f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod;
288+
}
289+
}
290+
return f[k];
291+
}
292+
};
120293
```
121294
122295
#### Go
123296
124297
```go
298+
func sumOfPower(nums []int, k int) int {
299+
const mod int = 1e9 + 7
300+
f := make([]int, k+1)
301+
f[0] = 1
302+
for _, x := range nums {
303+
for j := k; j >= 0; j-- {
304+
f[j] = f[j] * 2 % mod
305+
if j >= x {
306+
f[j] = (f[j] + f[j-x]) % mod
307+
}
308+
}
309+
}
310+
return f[k]
311+
}
312+
```
125313

314+
#### TypeScript
315+
316+
```ts
317+
function sumOfPower(nums: number[], k: number): number {
318+
const mod = 10 ** 9 + 7;
319+
const f: number[] = Array(k + 1).fill(0);
320+
f[0] = 1;
321+
for (const x of nums) {
322+
for (let j = k; ~j; --j) {
323+
f[j] = (f[j] * 2) % mod;
324+
if (j >= x) {
325+
f[j] = (f[j] + f[j - x]) % mod;
326+
}
327+
}
328+
}
329+
return f[k];
330+
}
126331
```
127332

128333
<!-- tabs:end -->

0 commit comments

Comments
 (0)