Class Solution
java.lang.Object
g2601_2700.s2656_maximum_sum_with_exactly_k_elements.Solution
2656 - Maximum Sum With Exactly K Elements.<p>Easy</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>. Your task is to perform the following operation <strong>exactly</strong> <code>k</code> times in order to maximize your score:</p>
<ol>
<li>Select an element <code>m</code> from <code>nums</code>.</li>
<li>Remove the selected element <code>m</code> from the array.</li>
<li>Add a new element with a value of <code>m + 1</code> to the array.</li>
<li>Increase your score by <code>m</code>.</li>
</ol>
<p>Return <em>the maximum score you can achieve after performing the operation exactly</em> <code>k</code> <em>times.</em></p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [1,2,3,4,5], k = 3</p>
<p><strong>Output:</strong> 18</p>
<p><strong>Explanation:</strong> We need to choose exactly 3 elements from nums to maximize the sum.</p>
<p>For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]</p>
<p>For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]</p>
<p>For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]</p>
<p>So, we will return 18. It can be proven, that 18 is the maximum answer that we can achieve.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [5,5,5], k = 2</p>
<p><strong>Output:</strong> 11</p>
<p><strong>Explanation:</strong> We need to choose exactly 2 elements from nums to maximize the sum.</p>
<p>For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]</p>
<p>For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]</p>
<p>So, we will return 11.</p>
<p>It can be proven, that 11 is the maximum answer that we can achieve.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>1 <= nums[i] <= 100</code></li>
<li><code>1 <= k <= 100</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maximizeSum
public int maximizeSum(int[] nums, int k)
-