{"id":7768,"date":"2020-12-06T11:36:21","date_gmt":"2020-12-06T19:36:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7768"},"modified":"2020-12-06T11:41:40","modified_gmt":"2020-12-06T19:41:40","slug":"leetcode-1679-max-number-of-k-sum-pairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1679-max-number-of-k-sum-pairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1679. Max Number of K-Sum Pairs"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>In one operation, you can pick two numbers from the array whose sum equals&nbsp;<code>k<\/code>&nbsp;and remove them from the array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the maximum number of operations you can perform on the array<\/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], k = 5\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Starting with nums = [1,2,3,4]:\n- Remove numbers 1 and 4, then nums = [2,3]\n- Remove numbers 2 and 3, then nums = []\nThere are no more pairs that sum up to 5, hence a total of 2 operations.<\/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 = [3,1,3,4,3], k = 6\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Starting with nums = [3,1,3,4,3]:\n- Remove the first two 3's, then nums = [1,4,3]\nThere are no more pairs that sum up to 6, hence a total of 1 operation.<\/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>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Frequency Map<\/strong><\/h2>\n\n\n\n<p>For each x, check freq[x] and freq[k &#8211; x]. Note: there is a special case when x + x == k.<\/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++\">\nclass Solution {\npublic:\n  int maxOperations(vector<int>& nums, int k) {\n    unordered_map<int, int> m;\n    int ans = 0;\n    for (int x : nums) ++m[x];\n    for (int x : nums) {      \n      if (m[x] < 1 || m[k - x] < 1 + (x + x == k)) continue;\n      --m[x];\n      --m[k - x];\n      ++ans;      \n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def maxOperations(self, nums: List[int], k: int) -> int:\n    m = defaultdict(int)\n    ans = 0\n    for x in nums: m[x] += 1\n    for x in nums:\n      if m[x] < 1 or m[k - x] < 1 + (x + x == k): continue\n      m[x] -= 1\n      m[k - x] -= 1\n      ans += 1\n    return ans        \n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Sort the number, start from i = 0, j = n - 1, compare s = nums[i] + nums[j] with k and move i, j accordingly.<\/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++\">\nclass Solution {\npublic:\n  int maxOperations(vector<int>& nums, int k) {\n    sort(begin(nums), end(nums));\n    int i = 0, j = nums.size() - 1;\n    int ans = 0;\n    while (i < j) {\n      const int s = nums[i] + nums[j];\n      if (s == k) {\n        ++ans;\n        ++i; --j;\n      } else if (s < k) {\n        ++i;\n      } else {\n        --j;\n      }\n    }\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. In one operation, you can pick two numbers from the array whose sum equals&nbsp;k&nbsp;and remove them from&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,677,175],"class_list":["post-7768","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-pair-sum","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7768","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=7768"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7768\/revisions"}],"predecessor-version":[{"id":7771,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7768\/revisions\/7771"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}