First Missing Positive LeetCode solution in Python

1
# Runtime: 295 ms (Top 90.42%) | Memory: 28 MB (Top 72.11%)
2
class Solution:
3
def firstMissingPositive(self, nums: List[int]) -> int:
4
mn = float("inf")
5
mx = 0
6
numsSet = set()
7

8
for i in range(len(nums) - 1, -1, -1):
9
if nums[i] > 0:
10
if nums[i] < mn:
11
mn = nums[i]
12
if nums[i] > mx:
13
mx = nums[i]
14
numsSet.add(nums[i])
15
del nums[i]
16

17
if mn >= 2:
18
return 1
19
if len(numsSet) == mx:
20
return mx + 1
21
for i in range(2, len(numsSet) + 1):
22
if i not in numsSet:
23
return i

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0