{"id":10022,"date":"2023-04-30T07:58:44","date_gmt":"2023-04-30T14:58:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10022"},"modified":"2023-04-30T07:59:23","modified_gmt":"2023-04-30T14:59:23","slug":"leetcode-2656-maximum-sum-with-exactly-k-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2656-maximum-sum-with-exactly-k-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2656. Maximum Sum With Exactly K Elements"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>. Your task is to perform the following operation&nbsp;<strong>exactly<\/strong>&nbsp;<code>k<\/code>&nbsp;times in order to maximize your score:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Select an element&nbsp;<code>m<\/code>&nbsp;from&nbsp;<code>nums<\/code>.<\/li><li>Remove the selected element&nbsp;<code>m<\/code>&nbsp;from the array.<\/li><li>Add a new element with a value of&nbsp;<code>m + 1<\/code>&nbsp;to the array.<\/li><li>Increase your score by&nbsp;<code>m<\/code>.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>the maximum score you can achieve after performing the operation exactly<\/em>&nbsp;<code>k<\/code>&nbsp;<em>times.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5], k = 3\n<strong>Output:<\/strong> 18\n<strong>Explanation:<\/strong> We need to choose exactly 3 elements from nums to maximize the sum.\nFor the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]\nFor the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]\nFor the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]\nSo, we will return 18.\nIt can be proven, that 18 is the maximum answer that we can achieve.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [5,5,5], k = 2\n<strong>Output:<\/strong> 11\n<strong>Explanation:<\/strong> We need to choose exactly 2 elements from nums to maximize the sum.\nFor the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]\nFor the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]\nSo, we will return 11.\nIt can be proven, that 11 is the maximum answer that we can achieve.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 100<\/code><\/li><li><code>1 &lt;= k &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Always to chose the largest element from the array.<\/p>\n\n\n\n<p>We can find the largest element of the array m, then the total score will be<br>m + (m + 1) + (m + 2) + &#8230; + (m + k &#8211; 1),<br>We can use summation formula of arithmetic sequence to compute that in O(1)<br>ans = (m + (m + k &#8211; 1)) * k \/ 2<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximizeSum(vector<int>& nums, int k) {\n    int m = *max_element(begin(nums), end(nums));\n    return (m + m + k - 1) * k \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;and an integer&nbsp;k. Your task is to perform the following operation&nbsp;exactly&nbsp;k&nbsp;times in order to maximize your score: Select an element&nbsp;m&nbsp;from&nbsp;nums. Remove&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[222,88,31],"class_list":["post-10022","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-easy","tag-greedy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10022"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10022\/revisions"}],"predecessor-version":[{"id":10024,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10022\/revisions\/10024"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}