Problem
To find the first missing positive number (from 1).
Algorithm
save the number smaller then len(list) and record it in their position of another list. Then ordered traversal on the list and find the first missing number.
Code
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
nLen = len(nums)
index = [-1] * (nLen + 1)
for i in range(nLen):
if nums[i] > 0 and nums[i] <= nLen:
index[nums[i]] = i
for i in range(1, nLen + 1):
if index[i] == -1:
return i
return nLen+1