LeetCode — First Missing Positive (Hard)

Steven Lu
1 min readSep 30, 2020

Given an unsorted integer array, find the smallest missing positive integer.

Example 1:

Input: [1,2,0]
Output: 3

Example 2:

Input: [3,4,-1,1]
Output: 2

Example 3:

Input: [7,8,9,11,12]
Output: 1

Solution:

The idea of this efficient approach is to apply swap method to maintain constant space in the fixed length of array, representing n fixed positive integers. When encountering a number, finding out its index position & then swap.

Time complexity: O(1)

--

--