/
0041_First_Missing_Positive.py
47 lines (42 loc) · 1.17 KB
/
0041_First_Missing_Positive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# sorting solution
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 1
nums.sort()
index = 1
ans = 1
for i in range(len(nums)):
if nums[i] > 0 and nums[i] >= index:
index += 1
if nums[i] > index:
ans = index
break
if index == nums[-1]:
ans = index + 1
return ans
# linear time, constant space solution
# using element value as index
class Solution:
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def remove_unwanted(A):
for i in range(len(A))[::-1]:
if A[i] < 1:
A.pop(i)
remove_unwanted(nums)
for i in range(len(nums)):
j = abs(nums[i]) - 1
if j < len(nums) and j >= 0:
nums[j] = -abs(nums[j])
for i in range(len(nums)):
if nums[i] > 0:
return i+1
return len(nums) + 1