Problem statement

https://https://binarysearch.com/problems/First-Missing-Positive-Sequel/

Solution

It is almost the same as BinarySearch 0007 First Missing Positive, we can reuse solution here.

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, nums):
        n = len(nums)
        for i in range(n):
            while nums[i]-1 in range(n) and nums[i] != nums[nums[i]-1]:
                nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
                
        return ([i == nums[i]-1 for i in range(n)] + [0]).index(False) + 1