-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_004.py
51 lines (40 loc) · 1.2 KB
/
problem_004.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
def get_positive_subset(array):
i = 0
j = len(array) - 1
while i < j:
if array[i] > 0 and array[j] <= 0:
array[i], array[j] = array[j], array[i]
i += 1
j -= 1
elif array[i] > 0:
j -= 1
else:
i += 1
# print("i: {}, j: {}".format(i, j))
# print("partitioned_array:", array)
pivot = i if array[i] > 0 else i + 1
return array[pivot:]
def get_missing_number(array):
if not array:
return 1
array = get_positive_subset(array)
array_len = len(array)
# print("array: {}".format(array))
if not array:
return 1
if max(array) == len(array):
return max(array) + 1
for num in array:
current_num = abs(num)
if (current_num - 1) < array_len:
array[current_num - 1] *= -1
# print("mutated_array: {}".format(array))
for i, num in enumerate(array):
if num > 0:
return i + 1
assert get_missing_number([3, 4, -1, 1]) == 2
assert get_missing_number([1, 2, 0]) == 3
assert get_missing_number([1, 2, 5]) == 3
assert get_missing_number([1]) == 2
assert get_missing_number([-1, -2]) == 1
assert get_missing_number([]) == 1