-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsolution.py
More file actions
21 lines (18 loc) · 595 Bytes
/
Copy pathsolution.py
File metadata and controls
21 lines (18 loc) · 595 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
# Time: O(n)
# Space: O(1)
def first_missing_positive(self, nums: list[int]) -> int:
n = len(nums)
# Place each number in its correct position
i = 0
while i < n:
correct_idx = nums[i] - 1
if 1 <= nums[i] <= n and nums[i] != nums[correct_idx]:
nums[i], nums[correct_idx] = nums[correct_idx], nums[i]
else:
i += 1
# Find the first missing positive
for i in range(n):
if nums[i] != i + 1:
return i + 1
return n + 1