|
52 | 52 |
|
53 | 53 | <!-- 这里可写通用的实现逻辑 -->
|
54 | 54 |
|
| 55 | +**方法一:动态规划** |
| 56 | + |
| 57 | +我们定义 $f[i][j]$ 表示前 $i$ 个数中选取若干个数,使得这若干个数的和恰好为 $j$ 的最长子序列的长度。初始时 $f[0][0]=0$,其余位置均为 $-\infty$。 |
| 58 | + |
| 59 | +对于 $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$。因此我们有状态转移方程: |
| 60 | + |
| 61 | +$$ |
| 62 | +f[i][j]=\max\{f[i-1][j],f[i-1][j-x]+1\} |
| 63 | +$$ |
| 64 | + |
| 65 | +最终答案为 $f[n][target]$,如果 $f[n][target]\le0$,则不存在和为 $target$ 的子序列,返回 $-1$。 |
| 66 | + |
| 67 | +时间复杂度 $O(n\times target)$,空间复杂度 $O(n\times target)$。其中 $n$ 为数组长度,而 $target$ 为目标值。 |
| 68 | + |
| 69 | +我们注意到 $f[i][j]$ 的状态只与 $f[i-1][\cdot]$ 有关,因此我们可以优化掉第一维,将空间复杂度优化到 $O(target)$。 |
| 70 | + |
55 | 71 | <!-- tabs:start -->
|
56 | 72 |
|
57 | 73 | ### **Python3**
|
58 | 74 |
|
59 | 75 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 76 |
|
61 | 77 | ```python
|
| 78 | +class Solution: |
| 79 | + def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int: |
| 80 | + n = len(nums) |
| 81 | + f = [[-inf] * (target + 1) for _ in range(n + 1)] |
| 82 | + f[0][0] = 0 |
| 83 | + for i, x in enumerate(nums, 1): |
| 84 | + for j in range(target + 1): |
| 85 | + f[i][j] = f[i - 1][j] |
| 86 | + if j >= x: |
| 87 | + f[i][j] = max(f[i][j], f[i - 1][j - x] + 1) |
| 88 | + return -1 if f[n][target] <= 0 else f[n][target] |
| 89 | +``` |
62 | 90 |
|
| 91 | +```python |
| 92 | +class Solution: |
| 93 | + def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int: |
| 94 | + f = [0] + [-inf] * target |
| 95 | + for x in nums: |
| 96 | + for j in range(target, x - 1, -1): |
| 97 | + f[j] = max(f[j], f[j - x] + 1) |
| 98 | + return -1 if f[-1] <= 0 else f[-1] |
63 | 99 | ```
|
64 | 100 |
|
65 | 101 | ### **Java**
|
66 | 102 |
|
67 | 103 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
68 | 104 |
|
69 | 105 | ```java
|
| 106 | +class Solution { |
| 107 | + public int lengthOfLongestSubsequence(List<Integer> nums, int target) { |
| 108 | + int n = nums.size(); |
| 109 | + int[][] f = new int[n + 1][target + 1]; |
| 110 | + final int inf = 1 << 30; |
| 111 | + for (int[] g : f) { |
| 112 | + Arrays.fill(g, -inf); |
| 113 | + } |
| 114 | + f[0][0] = 0; |
| 115 | + for (int i = 1; i <= n; ++i) { |
| 116 | + int x = nums.get(i - 1); |
| 117 | + for (int j = 0; j <= target; ++j) { |
| 118 | + f[i][j] = f[i - 1][j]; |
| 119 | + if (j >= x) { |
| 120 | + f[i][j] = Math.max(f[i][j], f[i - 1][j - x] + 1); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return f[n][target] <= 0 ? -1 : f[n][target]; |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
70 | 128 |
|
| 129 | +```java |
| 130 | +class Solution { |
| 131 | + public int lengthOfLongestSubsequence(List<Integer> nums, int target) { |
| 132 | + int[] f = new int[target + 1]; |
| 133 | + final int inf = 1 << 30; |
| 134 | + Arrays.fill(f, -inf); |
| 135 | + f[0] = 0; |
| 136 | + for (int x : nums) { |
| 137 | + for (int j = target; j >= x; --j) { |
| 138 | + f[j] = Math.max(f[j], f[j - x] + 1); |
| 139 | + } |
| 140 | + } |
| 141 | + return f[target] <= 0 ? -1 : f[target]; |
| 142 | + } |
| 143 | +} |
71 | 144 | ```
|
72 | 145 |
|
73 | 146 | ### **C++**
|
74 | 147 |
|
75 | 148 | ```cpp
|
| 149 | +class Solution { |
| 150 | +public: |
| 151 | + int lengthOfLongestSubsequence(vector<int>& nums, int target) { |
| 152 | + int n = nums.size(); |
| 153 | + int f[n + 1][target + 1]; |
| 154 | + memset(f, -0x3f, sizeof(f)); |
| 155 | + f[0][0] = 0; |
| 156 | + for (int i = 1; i <= n; ++i) { |
| 157 | + int x = nums[i - 1]; |
| 158 | + for (int j = 0; j <= target; ++j) { |
| 159 | + f[i][j] = f[i - 1][j]; |
| 160 | + if (j >= x) { |
| 161 | + f[i][j] = max(f[i][j], f[i - 1][j - x] + 1); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return f[n][target] <= 0 ? -1 : f[n][target]; |
| 166 | + } |
| 167 | +}; |
| 168 | +``` |
76 | 169 |
|
| 170 | +```cpp |
| 171 | +class Solution { |
| 172 | +public: |
| 173 | + int lengthOfLongestSubsequence(vector<int>& nums, int target) { |
| 174 | + int f[target + 1]; |
| 175 | + memset(f, -0x3f, sizeof(f)); |
| 176 | + f[0] = 0; |
| 177 | + for (int x : nums) { |
| 178 | + for (int j = target; j >= x; --j) { |
| 179 | + f[j] = max(f[j], f[j - x] + 1); |
| 180 | + } |
| 181 | + } |
| 182 | + return f[target] <= 0 ? -1 : f[target]; |
| 183 | + } |
| 184 | +}; |
77 | 185 | ```
|
78 | 186 |
|
79 | 187 | ### **Go**
|
80 | 188 |
|
81 | 189 | ```go
|
| 190 | +func lengthOfLongestSubsequence(nums []int, target int) int { |
| 191 | + n := len(nums) |
| 192 | + f := make([][]int, n+1) |
| 193 | + for i := range f { |
| 194 | + f[i] = make([]int, target+1) |
| 195 | + for j := range f[i] { |
| 196 | + f[i][j] = -(1 << 30) |
| 197 | + } |
| 198 | + } |
| 199 | + f[0][0] = 0 |
| 200 | + for i := 1; i <= n; i++ { |
| 201 | + x := nums[i-1] |
| 202 | + for j := 0; j <= target; j++ { |
| 203 | + f[i][j] = f[i-1][j] |
| 204 | + if j >= x { |
| 205 | + f[i][j] = max(f[i][j], f[i-1][j-x]+1) |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + if f[n][target] <= 0 { |
| 210 | + return -1 |
| 211 | + } |
| 212 | + return f[n][target] |
| 213 | +} |
| 214 | + |
| 215 | +func max(a, b int) int { |
| 216 | + if a > b { |
| 217 | + return a |
| 218 | + } |
| 219 | + return b |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +```go |
| 224 | +func lengthOfLongestSubsequence(nums []int, target int) int { |
| 225 | + f := make([]int, target+1) |
| 226 | + for i := range f { |
| 227 | + f[i] = -(1 << 30) |
| 228 | + } |
| 229 | + f[0] = 0 |
| 230 | + for _, x := range nums { |
| 231 | + for j := target; j >= x; j-- { |
| 232 | + f[j] = max(f[j], f[j-x]+1) |
| 233 | + } |
| 234 | + } |
| 235 | + if f[target] <= 0 { |
| 236 | + return -1 |
| 237 | + } |
| 238 | + return f[target] |
| 239 | +} |
| 240 | + |
| 241 | +func max(a, b int) int { |
| 242 | + if a > b { |
| 243 | + return a |
| 244 | + } |
| 245 | + return b |
| 246 | +} |
| 247 | +``` |
| 248 | + |
| 249 | +### **TypeScript** |
| 250 | + |
| 251 | +```ts |
| 252 | +function lengthOfLongestSubsequence(nums: number[], target: number): number { |
| 253 | + const n = nums.length; |
| 254 | + const f: number[][] = Array.from({ length: n + 1 }, () => Array(target + 1).fill(-Infinity)); |
| 255 | + f[0][0] = 0; |
| 256 | + for (let i = 1; i <= n; ++i) { |
| 257 | + const x = nums[i - 1]; |
| 258 | + for (let j = 0; j <= target; ++j) { |
| 259 | + f[i][j] = f[i - 1][j]; |
| 260 | + if (j >= x) { |
| 261 | + f[i][j] = Math.max(f[i][j], f[i - 1][j - x] + 1); |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + return f[n][target] <= 0 ? -1 : f[n][target]; |
| 266 | +} |
| 267 | +``` |
82 | 268 |
|
| 269 | +```ts |
| 270 | +function lengthOfLongestSubsequence(nums: number[], target: number): number { |
| 271 | + const f: number[] = Array(target + 1).fill(-Infinity); |
| 272 | + f[0] = 0; |
| 273 | + for (const x of nums) { |
| 274 | + for (let j = target; j >= x; --j) { |
| 275 | + f[j] = Math.max(f[j], f[j - x] + 1); |
| 276 | + } |
| 277 | + } |
| 278 | + return f[target] <= 0 ? -1 : f[target]; |
| 279 | +} |
83 | 280 | ```
|
84 | 281 |
|
85 | 282 | ### **...**
|
|
0 commit comments