{"id":9550,"date":"2022-03-08T03:25:58","date_gmt":"2022-03-08T11:25:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9550"},"modified":"2022-03-08T03:40:21","modified_gmt":"2022-03-08T11:40:21","slug":"leetcode-2195-append-k-integers-with-minimal-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2195-append-k-integers-with-minimal-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2195. Append K Integers With Minimal Sum"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>. Append&nbsp;<code>k<\/code>&nbsp;<strong>unique positive<\/strong>&nbsp;integers that do&nbsp;<strong>not<\/strong>&nbsp;appear in&nbsp;<code>nums<\/code>&nbsp;to&nbsp;<code>nums<\/code>&nbsp;such that the resulting total sum is&nbsp;<strong>minimum<\/strong>.<\/p>\n\n\n\n<p>Return<em>&nbsp;the sum of the<\/em>&nbsp;<code>k<\/code>&nbsp;<em>integers appended to<\/em>&nbsp;<code>nums<\/code>.<\/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,4,25,10,25], k = 2\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The two unique positive integers that do not appear in nums which we append are 2 and 3.\nThe resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum.\nThe sum of the two integers appended is 2 + 3 = 5, so we return 5.<\/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,6], k = 6\n<strong>Output:<\/strong> 25\n<strong>Explanation:<\/strong> The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.\nThe resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum. \nThe sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.\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;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= 10<sup>8<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + Math, fill the gap<\/strong><\/h2>\n\n\n\n<p>Sort all the numbers and remove duplicated ones, and fill the gap between two neighboring numbers.<br>e.g. [15, 3, 8, 8] => sorted = [3, 8, 15] <br>fill 0->3, 1,2, sum = ((0 + 1) + (3-1)) * (3-0-1) \/ 2 = 3<br>fill 3->8, 4, 5, 6, 7, <meta charset=\"utf-8\">sum = ((3 + 1) + (8-1)) * (8-3-1) \/ 2 = 22<br>fill 8->15, 9, 10, &#8230;, 14, &#8230;<br>fill 15->inf, 16, 17, &#8230;<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<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  long long minimalKSum(vector<int>& nums, int k) {    \n    sort(begin(nums), end(nums));\n    long long ans = 0;\n    long long p = 0;\n    for (int c : nums) {\n      if (c == p) continue;\n      long long n = min((long long)k, c - p - 1);\n      ans += (p + 1 + p + n) * n \/ 2; \n      k -= n;\n      p = c;\n    }\n    ans += (p + 1 + p + k) * k \/ 2;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;nums&nbsp;and an integer&nbsp;k. Append&nbsp;k&nbsp;unique positive&nbsp;integers that do&nbsp;not&nbsp;appear in&nbsp;nums&nbsp;to&nbsp;nums&nbsp;such that the resulting total sum is&nbsp;minimum. Return&nbsp;the sum of the&nbsp;k&nbsp;integers appended to&nbsp;nums.&#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":[20,88,15],"class_list":["post-9550","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-array","tag-greedy","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9550","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=9550"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9550\/revisions"}],"predecessor-version":[{"id":9557,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9550\/revisions\/9557"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}