{"id":7212,"date":"2020-08-09T00:43:58","date_gmt":"2020-08-09T07:43:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7212"},"modified":"2020-08-09T01:28:51","modified_gmt":"2020-08-09T08:28:51","slug":"leetcode-1539-kth-missing-positive-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1539-kth-missing-positive-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1539. Kth Missing Positive Number"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>arr<\/code>&nbsp;of positive integers&nbsp;sorted in a&nbsp;<strong>strictly increasing order<\/strong>, and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p><em>Find the&nbsp;<\/em><code>k<sup>th<\/sup><\/code><em>&nbsp;positive integer that is missing from this 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> arr = [2,3,4,7,11], k = 5\n<strong>Output:<\/strong> 9\n<strong>Explanation: <\/strong>The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5<sup>th<\/sup>&nbsp;missing positive integer is 9.\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> arr = [1,2,3,4], k = 2\n<strong>Output:<\/strong> 6\n<strong>Explanation: <\/strong>The missing positive integers are [5,6,7,...]. The 2<sup>nd<\/sup> missing positive integer is 6.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 1000<\/code><\/li><li><code>1 &lt;= k &lt;= 1000<\/code><\/li><li><code>arr[i] &lt; arr[j]<\/code>&nbsp;for&nbsp;<code>1 &lt;= i &lt; j &lt;= arr.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: HashTable<\/strong><\/h2>\n\n\n\n<p>Store all the elements into a hashtable, and check from 1 to max(arr).<\/p>\n\n\n\n<p>Time complexity: O(max(arr)) ~ O(1000)<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 findKthPositive(vector<int>& arr, int k) {\n    unordered_set<int> s(begin(arr), end(arr));\n    for (int i = 1; i <= arr.back(); ++i) {\n      if (!s.count(i)) --k;\n      if (k == 0) return i;\n    }\n    return arr.back() + k;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Binary Search<\/strong><\/h2>\n\n\n\n<p>We can find the smallest index l using binary search, s.t.<br>arr[l] - l + 1 >= k<br>which means we missed at least k numbers at index l.<br>And the answer will be l + k.<\/p>\n\n\n\n<p>Time complexity: O(logn)<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 findKthPositive(vector<int>& arr, int k) {\n    int l = 0;\n    int r = arr.size();\n    while (l < r) {\n      int m = l + (r - l) \/ 2;\n      if (arr[m] - (m + 1) >= k)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l + k;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\nclass Solution {\n  public int findKthPositive(int[] arr, int k) {\n    int l = 0;\n    int r = arr.length;\n    while (l < r) {\n      int m = l + (r - l) \/ 2;\n      if (arr[m] - (m + 1) >= k)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l + k;\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 findKthPositive(self, arr: List[int], k: int) -> int:\n    l, r = 0, len(arr) \n    while l < r:\n      m = l + (r - l) \/\/ 2\n      if arr[m] - (m + 1) >= k:\n        r = m\n      else:\n        l = m + 1\n    return l + k\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;arr&nbsp;of positive integers&nbsp;sorted in a&nbsp;strictly increasing order, and an integer&nbsp;k. Find the&nbsp;kth&nbsp;positive integer that is missing from this array. Example 1: Input: arr&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[20,52,222,82],"class_list":["post-7212","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-array","tag-binary-search","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7212","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=7212"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7212\/revisions"}],"predecessor-version":[{"id":7215,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7212\/revisions\/7215"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}