<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>marking Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/marking/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/marking/</link>
	<description></description>
	<lastBuildDate>Sat, 27 Nov 2021 02:40:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>marking Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/marking/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 41. First Missing Positive</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 27 Nov 2021 02:40:37 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[marking]]></category>
		<category><![CDATA[missing]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8801</guid>

					<description><![CDATA[<p>Given an unsorted integer array&#160;nums, return the smallest missing positive integer. You must implement an algorithm that runs in&#160;O(n)&#160;time and uses constant extra space. Example&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/">花花酱 LeetCode 41. First Missing Positive</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an unsorted integer array&nbsp;<code>nums</code>, return the smallest missing positive integer.</p>



<p>You must implement an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and uses constant extra space.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,0]
<strong>Output:</strong> 3
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,4,-1,1]
<strong>Output:</strong> 2
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [7,8,9,11,12]
<strong>Output:</strong> 1
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Marking</strong></h2>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int firstMissingPositive(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    for (int&amp; x : nums)
      if (x &lt;= 0) x = INT_MAX;
    
    for (int x : nums) {
      x = abs(x);
      if (x &gt;= 1 &amp;&amp; x &lt;= n &amp;&amp; nums[x - 1] &gt; 0)
        nums[x - 1] *= -1;
    }
    
    for (int i = 0; i &lt; n; ++i)
      if (nums[i] &gt; 0)
        return i + 1;
    
    return n + 1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/">花花酱 LeetCode 41. First Missing Positive</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-41-first-missing-positive/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
