1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/和为目标值的最长子序列的长度 [length-of-the-longest-subsequence-that-sums-to-target].html
2023-11-03 23:14:24 +08:00

42 lines
1.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>target</code>&nbsp;</p>
<p>返回和为 <code>target</code>&nbsp;<code>nums</code>&nbsp;子序列中,子序列&nbsp;<strong>长度的最大值&nbsp;</strong>。如果不存在和为 <code>target</code>&nbsp;的子序列,返回 <code>-1</code>&nbsp;</p>
<p><strong>子序列</strong> 指的是从原数组中删除一些或者不删除任何元素后,剩余元素保持原来的顺序构成的数组。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>nums = [1,2,3,4,5], target = 9
<b>输出:</b>3
<b>解释:</b>总共有 3 个子序列的和为 9 [4,5] [1,3,5] 和 [2,3,4] 。最长的子序列是 [1,3,5] 和 [2,3,4] 。所以答案为 3 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>nums = [4,1,3,2,1,5], target = 7
<b>输出:</b>4
<strong>解释:</strong>总共有 5 个子序列的和为 7 [4,3] [4,1,2] [4,2,1] [1,1,5] 和 [1,3,2,1] 。最长子序列为 [1,3,2,1] 。所以答案为 4 。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>nums = [1,1,5,4,5], target = 3
<b>输出:</b>-1
<b>解释:</b>无法得到和为 3 的子序列。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
<li><code>1 &lt;= target &lt;= 1000</code></li>
</ul>