-
Notifications
You must be signed in to change notification settings - Fork 1
/
41. First Missing Positive.py
67 lines (56 loc) · 1.87 KB
/
41. 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import List
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n):
j = nums[i] - 1
while j < n and j >= 0:
nums[j],nums[i] = nums[i], nums[j]
j = nums[i] - 1
if nums[i] == i + 1:
break
for i in range(n):
if i + 1 != nums[i]:
return i + 1
return n + 1
nums = [1,1] #breaks this!!
out = Solution().firstMissingPositive(nums)
print(out)
"""
God Demn it, the good solution is so similar to my solution! To the last solution - not this one.
Best time is like mine - attached:
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
# Replace all the non positive numbers with a special number n+1
n = len(nums)
for i in range(n):
if nums[i] <= 0:
nums[i] = n+1
# print(nums)
# Try to put the elements in their correct locations. Put k in index k-1
i = 0
while i < n:
j = nums[i] - 1
if j < n and nums[i] != nums[j]:
nums[i], nums[j] = nums[j], nums[i]
else:
i+=1
# print(nums)
# Return the (index+1) where number is mismatching with its location
for i in range(n):
if nums[i] != i+1:
return i+1
# If all the elements are placed at their correct locations, return n+1
return n+1
This is average time... So similar to currect version.
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
nums_set=set(nums)
max_val=max(nums)
if max_val<=0:
return 1
for i in range(1,max_val):
if i not in nums_set:
return i
return max_val+1
"""