{"id":10085,"date":"2023-08-26T09:40:51","date_gmt":"2023-08-26T16:40:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10085"},"modified":"2023-08-26T09:42:23","modified_gmt":"2023-08-26T16:42:23","slug":"leetcode-2829-determine-the-minimum-sum-of-a-k-avoiding-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2829-determine-the-minimum-sum-of-a-k-avoiding-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2829. Determine the Minimum Sum of a k-avoiding Array"},"content":{"rendered":"\n<p>You are given two integers,&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>An array of&nbsp;<strong>distinct<\/strong>&nbsp;positive integers is called a&nbsp;<strong>k-avoiding<\/strong>&nbsp;array if there does not exist any pair of distinct elements that sum to&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;possible sum of a k-avoiding array of length&nbsp;<\/em><code>n<\/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> n = 5, k = 4\n<strong>Output:<\/strong> 18\n<strong>Explanation:<\/strong> Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18.\nIt can be proven that there is no k-avoiding array with a sum less than 18.\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> n = 2, k = 6\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> We can construct the array [1,2], which has a sum of 3.\nIt can be proven that there is no k-avoiding array with a sum less than 3.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n, k &lt;= 50<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Greedy + HashTable<\/strong><\/h2>\n\n\n\n<p>Always choose the smallest possible number starting from 1.<\/p>\n\n\n\n<p>Add all chosen numbers into a hashtable. For a new candidate i, check whether k &#8211; i is already in the hashtable.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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 minimumSum(int n, int k) {    \n    int sum = 0;\n    unordered_set<int> s;\n    for (int i = 1; s.size() != n; ++i) {\n      if (s.count(k - i)) continue;\n      sum += i;\n      s.insert(i);\n    }\n    return sum;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integers,&nbsp;n&nbsp;and&nbsp;k. An array of&nbsp;distinct&nbsp;positive integers is called a&nbsp;k-avoiding&nbsp;array if there does not exist any pair of distinct elements that sum to&nbsp;k.&#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":[88,82,177],"class_list":["post-10085","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10085","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=10085"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10085\/revisions"}],"predecessor-version":[{"id":10087,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10085\/revisions\/10087"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}