Problem statement

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

Solution

Equal to Leetcode Leetcode 0041. First Missing Positive.

Complexity

O(n) for time and 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