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)/最接近目标值的子序列和 [closest-subsequence-sum].html
2022-03-29 12:43:11 +08:00

42 lines
1.5 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>给你一个整数数组 <code>nums</code> 和一个目标值 <code>goal</code></p>
<p>你需要从 <code>nums</code> 中选出一个子序列,使子序列元素总和最接近 <code>goal</code> 。也就是说,如果子序列元素和为 <code>sum</code> ,你需要 <strong>最小化绝对差</strong> <code>abs(sum - goal)</code></p>
<p>返回 <code>abs(sum - goal)</code> 可能的 <strong>最小值</strong></p>
<p>注意,数组的子序列是通过移除原始数组中的某些元素(可能全部或无)而形成的数组。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [5,-7,3,5], goal = 6
<strong>输出:</strong>0
<strong>解释:</strong>选择整个数组作为选出的子序列,元素和为 6 。
子序列和与目标值相等,所以绝对差为 0 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [7,-9,15,-2], goal = -5
<strong>输出:</strong>1
<strong>解释:</strong>选出子序列 [7,-9,-2] ,元素和为 -4 。
绝对差为 abs(-4 - (-5)) = abs(1) = 1 ,是可能的最小值。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3], goal = -7
<strong>输出:</strong>7
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 40</code></li>
<li><code>-10<sup>7</sup> &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= goal &lt;= 10<sup>9</sup></code></li>
</ul>