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)/K 次取反后最大化的数组和 [maximize-sum-of-array-after-k-negations].html
2022-03-29 12:43:11 +08:00

46 lines
1.3 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>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,按以下方法修改该数组:</p>
<ul>
<li>选择某个下标 <code>i</code>&nbsp;并将 <code>nums[i]</code> 替换为 <code>-nums[i]</code></li>
</ul>
<p>重复这个过程恰好 <code>k</code> 次。可以多次选择同一个下标 <code>i</code></p>
<p>以这种方式修改数组后,返回数组 <strong>可能的最大和</strong></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [4,2,3], k = 1
<strong>输出:</strong>5
<strong>解释:</strong>选择下标 1 nums 变为 [4,-2,3] 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [3,-1,0,2], k = 3
<strong>输出:</strong>6
<strong>解释:</strong>选择下标 (1, 2, 2) nums 变为 [3,1,0,2] 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [2,-3,-1,5,-4], k = 2
<strong>输出:</strong>13
<strong>解释:</strong>选择下标 (1, 4) nums 变为 [2,3,-1,5,4] 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
</ul>