题目描述
给你一个下标从 0 开始的整数数组 nums
和一个整数 target
。
返回和为 target
的 nums
子序列中,子序列 长度的最大值 。如果不存在和为 target
的子序列,返回 -1
。
子序列 指的是从原数组中删除一些或者不删除任何元素后,剩余元素保持原来的顺序构成的数组。
示例 1:
输入:nums = [1,2,3,4,5], target = 9
输出:3
解释:总共有 3 个子序列的和为 9 :[4,5] ,[1,3,5] 和 [2,3,4] 。最长的子序列是 [1,3,5] 和 [2,3,4] 。所以答案为 3 。
示例 2:
输入:nums = [4,1,3,2,1,5], target = 7
输出:4
解释:总共有 5 个子序列的和为 7 :[4,3] ,[4,1,2] ,[4,2,1] ,[1,1,5] 和 [1,3,2,1] 。最长子序列为 [1,3,2,1] 。所以答案为 4 。
示例 3:
输入:nums = [1,1,5,4,5], target = 3
输出:-1
解释:无法得到和为 3 的子序列。
提示:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000
1 <= target <= 1000
解法
方法一:动态规划
我们定义 $f[i][j]$ 表示前 $i$ 个数中选取若干个数,使得这若干个数的和恰好为 $j$ 的最长子序列的长度。初始时 $f[0][0]=0$,其余位置均为 $-\infty$。
对于 $f[i][j]$,我们考虑第 $i$ 个数 $x$,如果不选取 $x$,那么 $f[i][j]=f[i-1][j]$;如果选取 $x$,那么 $f[i][j]=f[i-1][j-x]+1$,其中 $j\ge x$。因此我们有状态转移方程:
$$
f[i][j]=\max{f[i-1][j],f[i-1][j-x]+1}
$$
最终答案为 $f[n][target]$,如果 $f[n][target]\le0$,则不存在和为 $target$ 的子序列,返回 $-1$。
时间复杂度 $O(n\times target)$,空间复杂度 $O(n\times target)$。其中 $n$ 为数组长度,而 $target$ 为目标值。
我们注意到 $f[i][j]$ 的状态只与 $f[i-1][\cdot]$ 有关,因此我们可以优化掉第一维,将空间复杂度优化到 $O(target)$。
| class Solution:
def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
n = len(nums)
f = [[-inf] * (target + 1) for _ in range(n + 1)]
f[0][0] = 0
for i, x in enumerate(nums, 1):
for j in range(target + 1):
f[i][j] = f[i - 1][j]
if j >= x:
f[i][j] = max(f[i][j], f[i - 1][j - x] + 1)
return -1 if f[n][target] <= 0 else f[n][target]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | class Solution {
public int lengthOfLongestSubsequence(List<Integer> nums, int target) {
int n = nums.size();
int[][] f = new int[n + 1][target + 1];
final int inf = 1 << 30;
for (int[] g : f) {
Arrays.fill(g, -inf);
}
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
int x = nums.get(i - 1);
for (int j = 0; j <= target; ++j) {
f[i][j] = f[i - 1][j];
if (j >= x) {
f[i][j] = Math.max(f[i][j], f[i - 1][j - x] + 1);
}
}
}
return f[n][target] <= 0 ? -1 : f[n][target];
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class Solution {
public:
int lengthOfLongestSubsequence(vector<int>& nums, int target) {
int n = nums.size();
int f[n + 1][target + 1];
memset(f, -0x3f, sizeof(f));
f[0][0] = 0;
for (int i = 1; i <= n; ++i) {
int x = nums[i - 1];
for (int j = 0; j <= target; ++j) {
f[i][j] = f[i - 1][j];
if (j >= x) {
f[i][j] = max(f[i][j], f[i - 1][j - x] + 1);
}
}
}
return f[n][target] <= 0 ? -1 : f[n][target];
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | func lengthOfLongestSubsequence(nums []int, target int) int {
n := len(nums)
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, target+1)
for j := range f[i] {
f[i][j] = -(1 << 30)
}
}
f[0][0] = 0
for i := 1; i <= n; i++ {
x := nums[i-1]
for j := 0; j <= target; j++ {
f[i][j] = f[i-1][j]
if j >= x {
f[i][j] = max(f[i][j], f[i-1][j-x]+1)
}
}
}
if f[n][target] <= 0 {
return -1
}
return f[n][target]
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | function lengthOfLongestSubsequence(nums: number[], target: number): number {
const n = nums.length;
const f: number[][] = Array.from({ length: n + 1 }, () => Array(target + 1).fill(-Infinity));
f[0][0] = 0;
for (let i = 1; i <= n; ++i) {
const x = nums[i - 1];
for (let j = 0; j <= target; ++j) {
f[i][j] = f[i - 1][j];
if (j >= x) {
f[i][j] = Math.max(f[i][j], f[i - 1][j - x] + 1);
}
}
}
return f[n][target] <= 0 ? -1 : f[n][target];
}
|
方法二
| class Solution:
def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
f = [0] + [-inf] * target
for x in nums:
for j in range(target, x - 1, -1):
f[j] = max(f[j], f[j - x] + 1)
return -1 if f[-1] <= 0 else f[-1]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution {
public int lengthOfLongestSubsequence(List<Integer> nums, int target) {
int[] f = new int[target + 1];
final int inf = 1 << 30;
Arrays.fill(f, -inf);
f[0] = 0;
for (int x : nums) {
for (int j = target; j >= x; --j) {
f[j] = Math.max(f[j], f[j - x] + 1);
}
}
return f[target] <= 0 ? -1 : f[target];
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution {
public:
int lengthOfLongestSubsequence(vector<int>& nums, int target) {
int f[target + 1];
memset(f, -0x3f, sizeof(f));
f[0] = 0;
for (int x : nums) {
for (int j = target; j >= x; --j) {
f[j] = max(f[j], f[j - x] + 1);
}
}
return f[target] <= 0 ? -1 : f[target];
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | func lengthOfLongestSubsequence(nums []int, target int) int {
f := make([]int, target+1)
for i := range f {
f[i] = -(1 << 30)
}
f[0] = 0
for _, x := range nums {
for j := target; j >= x; j-- {
f[j] = max(f[j], f[j-x]+1)
}
}
if f[target] <= 0 {
return -1
}
return f[target]
}
|
| function lengthOfLongestSubsequence(nums: number[], target: number): number {
const f: number[] = Array(target + 1).fill(-Infinity);
f[0] = 0;
for (const x of nums) {
for (let j = target; j >= x; --j) {
f[j] = Math.max(f[j], f[j - x] + 1);
}
}
return f[target] <= 0 ? -1 : f[target];
}
|