Class Solution
java.lang.Object
g1001_1100.s1005_maximize_sum_of_array_after_k_negations.Solution
1005 - Maximize Sum Of Array After K Negations.<p>Easy</p>
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, modify the array in the following way:</p>
<ul>
<li>choose an index <code>i</code> and replace <code>nums[i]</code> with <code>-nums[i]</code>.</li>
</ul>
<p>You should apply this process exactly <code>k</code> times. You may choose the same index <code>i</code> multiple times.</p>
<p>Return <em>the largest possible sum of the array after modifying it in this way</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [4,2,3], k = 1</p>
<p><strong>Output:</strong> 5</p>
<p><strong>Explanation:</strong> Choose index 1 and nums becomes [4,-2,3].</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [3,-1,0,2], k = 3</p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong> Choose indices (1, 2, 2) and nums becomes [3,1,0,2].</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [2,-3,-1,5,-4], k = 2</p>
<p><strong>Output:</strong> 13</p>
<p><strong>Explanation:</strong> Choose indices (1, 4) and nums becomes [2,3,-1,5,4].</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
<li><code>1 <= k <= 10<sup>4</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
largestSumAfterKNegations
public int largestSumAfterKNegations(int[] nums, int k)
-