{"id":8801,"date":"2021-11-26T18:40:37","date_gmt":"2021-11-27T02:40:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8801"},"modified":"2021-11-26T18:40:59","modified_gmt":"2021-11-27T02:40:59","slug":"leetcode-41-first-missing-positive","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-41-first-missing-positive\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 41. First Missing Positive"},"content":{"rendered":"\n<p>Given an unsorted integer array&nbsp;<code>nums<\/code>, return the smallest missing positive integer.<\/p>\n\n\n\n<p>You must implement an algorithm that runs in&nbsp;<code>O(n)<\/code>&nbsp;time and uses constant extra space.<\/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,0]\n<strong>Output:<\/strong> 3\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 = [3,4,-1,1]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [7,8,9,11,12]\n<strong>Output:<\/strong> 1\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;= 5 * 10<sup>5<\/sup><\/code><\/li><li><code>-2<sup>31<\/sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31<\/sup>&nbsp;- 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Marking<\/strong><\/h2>\n\n\n\n<p>First pass, marking nums[i] to INT_MAX if nums[i] &lt;= 0<br>Second pass, use a negative number to mark the presence of a number x at nums[x &#8211; 1]<br>Third pass, the first positive number is the missing index i, return i +1<br>If not found return n + 1.<\/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 firstMissingPositive(vector<int>& nums) {\n    const int n = nums.size();\n    for (int& x : nums)\n      if (x <= 0) x = INT_MAX;\n    \n    for (int x : nums) {\n      x = abs(x);\n      if (x >= 1 && x <= n &#038;&#038; nums[x - 1] > 0)\n        nums[x - 1] *= -1;\n    }\n    \n    for (int i = 0; i < n; ++i)\n      if (nums[i] > 0)\n        return i + 1;\n    \n    return n + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an unsorted integer array&nbsp;nums, return the smallest missing positive integer. You must implement an algorithm that runs in&nbsp;O(n)&nbsp;time and uses constant extra space. Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,217,734,64],"class_list":["post-8801","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-hard","tag-marking","tag-missing","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8801","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=8801"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8801\/revisions"}],"predecessor-version":[{"id":8803,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8801\/revisions\/8803"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}