-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfirst-missing-positive.py
110 lines (85 loc) · 3.43 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from typing import List
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
# Move all non-positive numbers to the end
offset = len(nums)
for pos in range(len(nums)):
if pos >= offset:
break
if nums[pos] <= 0:
offset -= 1
nums[pos], nums[offset] = nums[offset], nums[pos]
# Move all the numbers to positions representing their values
# if possible
for pos in range(offset):
while (
nums[pos] < offset
and nums[pos] > 0
and nums[pos] != pos + 1
and nums[pos] != nums[nums[pos] - 1]
):
num = nums[pos]
nums[pos], nums[num - 1] = nums[num - 1], nums[pos]
# Find a missing element
missing = 1
for num in nums:
if num > 0:
if num == missing:
missing += 1
else:
break
return missing
def firstMissingPositiveIncorrectQuickselect(self, nums: List[int]) -> int:
# Works only if there are none repetitions
negatives = sum(1 for num in nums if num <= 0)
left, right = 0, len(nums)
max_left = 0
while left < right:
pivot = nums[right - 1]
offset = 0
for pos in range(left, right):
if nums[pos] <= pivot:
max_left = max(max_left, nums[pos])
nums[pos], nums[left + offset] = nums[left + offset], nums[pos]
offset += 1
if max_left == left + offset - negatives:
left = left + offset
else:
right = left + offset - 1
return nums[left] + 1
def firstMissingPositiveOld(self, nums: List[int]) -> int:
MAX_BUCKETS_SIZE = 1000
begin, end = float("+inf"), float("-inf")
for num in nums:
if num > 0:
begin = min(begin, num)
end = max(end, num)
# Gap at the beginning, 1 is the only possible answer
if begin > 1:
return 1
while begin < end - 1:
# Fit all elements into at most MAX_BUCKETS_SIZE buckets
buckets_count = min((end - begin), MAX_BUCKETS_SIZE) + 1
bucket_size = (end - begin) // (buckets_count - 1)
buckets = [0] * buckets_count
# Update counts of elements in each bucket
for num in nums:
if begin <= num <= end:
buckets[(num - begin) // bucket_size] += 1
# Find the first bucket that has less than bucket_size elements
for bucket, count in enumerate(buckets):
if count < bucket_size:
# Update begin and end with this bucket to continue search
# for the missing element here
begin = begin + bucket * bucket_size
end = begin + bucket_size
# This is an element we're looking for, since we have 0
# elements in the bucket of size 1
if bucket_size == 1:
return begin
break
# Bucket size is 1 and no gaps found
if bucket_size == 1:
break
# No gaps found, return the element next to the last element
return end + 1