Skip to content

Commit 77c0b0c

Browse files
author
Мето Трајковски
committed
Added one more solution for find_first_missing_positive.py
1 parent 40cfcc0 commit 77c0b0c

7 files changed

Lines changed: 75 additions & 33 deletions

Arrays/find_first_missing_positive.py

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,62 @@
1313
Output: 3
1414
1515
=========================================
16-
Play with indicies and mark them, a marked index means that the number equals to that index exist in the array.
16+
Move all values to their positions (val position = val - 1), in the end find the first
17+
position which doesn't have the needed value.
18+
Time Complexity: O(N) , maybe nested loops look like O(N^2) but that not true
19+
Space Complexity: O(1)
20+
Play with indicies and mark them (make it negative),
21+
a marked index means that the number equals to that index exist in the array.
1722
Time Complexity: O(N)
1823
Space Complexity: O(1)
1924
'''
2025

2126

22-
############
23-
# Solution #
24-
############
27+
##############
28+
# Solution 1 #
29+
##############
30+
31+
def find_first_missing_1(a):
32+
n = len(a)
33+
34+
for i in range(n):
35+
while (a[i] > 0) and (a[i] <= n):
36+
swap = a[i] - 1
37+
if a[i] == a[swap]:
38+
break
39+
40+
# swap elements
41+
temp = a[i]
42+
a[i] = a[swap]
43+
a[swap] = temp
44+
45+
for i in range(n):
46+
if a[i] - 1 != i:
47+
return i + 1
48+
49+
return n + 1
50+
2551

26-
def find_first_missing(a):
52+
##############
53+
# Solution 2 #
54+
##############
55+
56+
def find_first_missing_2(a):
2757
n = len(a)
2858

2959
# eliminate all zeros and all negative numbers
3060
for i in range(n):
31-
if a[i] < 1:
32-
a[i] = n + 1 # those elements aren't used later
61+
if a[i] <= 0:
62+
a[i] = n + 1 # those values won't be used later
3363

3464
# find all numbers in the range and mark all numbers at those positions as negative numbers
35-
for i in range(len(a)):
65+
for i in range(n):
3666
idx = abs(a[i]) - 1
3767
if idx >= n:
3868
continue
39-
val = a[idx]
4069

41-
if val > 0:
42-
# mark element as found for the first time
43-
a[idx] = -val
70+
# mark the element as found
71+
a[idx] = -abs(a[idx])
4472

4573
# find the first non-negative position
4674
for i in range(n):
@@ -57,34 +85,47 @@ def find_first_missing(a):
5785
# Test 1
5886
# Correct result => 1
5987
test = [-1, 2, 3]
60-
print(find_first_missing(test))
88+
print(find_first_missing_1(list(test))) # make a copy, the list will be changed inside the function
89+
print(find_first_missing_2(list(test)))
6190

6291
# Test 2
6392
# Correct result => 2
6493
test = [3, 4, -1, 1]
65-
print(find_first_missing(test))
94+
print(find_first_missing_1(list(test)))
95+
print(find_first_missing_2(list(test)))
6696

6797
# Test 3
6898
# Correct result => 3
6999
test = [1, 2, 0]
70-
print(find_first_missing(test))
100+
print(find_first_missing_1(list(test)))
101+
print(find_first_missing_2(list(test)))
71102

72103
# Test 4
73104
# Correct result => 4
74105
test = [1, 2, 3]
75-
print(find_first_missing(test))
106+
print(find_first_missing_1(list(test)))
107+
print(find_first_missing_2(list(test)))
76108

77109
# Test 5
78110
# Correct result => 5
79111
test = [-4, -1, -3, -1]
80-
print(find_first_missing(test))
112+
print(find_first_missing_1(list(test)))
113+
print(find_first_missing_2(list(test)))
81114

82115
# Test 6
83116
# Correct result => 6
84117
test = [2, 1, 2, -1, 0, 20]
85-
print(find_first_missing(test))
118+
print(find_first_missing_1(list(test)))
119+
print(find_first_missing_2(list(test)))
86120

87121
# Test 7
88122
# Correct result => 7
89123
test = [1, 2, 5, 5, 1, 2]
90-
print(find_first_missing(test))
124+
print(find_first_missing_1(list(test)))
125+
print(find_first_missing_2(list(test)))
126+
127+
# Test 8
128+
# Correct result => 4
129+
test = [1, 2, 3, 5, 1, 2, 3, 3]
130+
print(find_first_missing_1(list(test)))
131+
print(find_first_missing_2(list(test)))

Arrays/find_peak_element.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
77
You may imagine that nums[-1] = nums[n] = -∞.
88
9-
Input: [1,2,3,1]
9+
Input: [1, 2, 3, 1]
1010
Output: 2
1111
Output explanation: 3 is a peak element and your function should return the index number 2.
1212
13-
Input: [1,2,1,3,5,6,4]
13+
Input: [1, 2, 1, 3, 5, 6, 4]
1414
Output: 1 or 5
1515
Output explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
1616
@@ -49,8 +49,8 @@ def find_peak_element(nums):
4949

5050
# Test 1
5151
# Correct result => 2
52-
print(find_peak_element([1,2,3,1]))
52+
print(find_peak_element([1, 2, 3, 1]))
5353

5454
# Test 2
5555
# Correct result => 1 or 5
56-
print(find_peak_element([1,2,1,3,5,6,4]))
56+
print(find_peak_element([1, 2, 1, 3, 5, 6, 4]))

Arrays/min_swaps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ def min_swaps(a):
3333
for i in range(n):
3434
# swap the elements till the right element isn't found
3535
while a[i] - 1 != i:
36+
swap = a[i] - 1
3637
# swap the elements
37-
tmp = a[a[i] - 1]
38-
a[a[i] - 1] = a[i]
38+
tmp = a[swap]
39+
a[swap] = a[i]
3940
a[i] = tmp
4041

4142
swaps += 1

Arrays/product_of_array_except_self.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
44
Given an array nums of n integers where n > 1,
55
return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
6-
76
Note: Please solve it without division and in O(n).
8-
97
Follow up:
108
Could you solve it with constant space complexity?
119
(The output array does not count as extra space for the purpose of space complexity analysis.)
1210
13-
Input: [1,2,3,4]
14-
Output: [24,12,8,6]
11+
Input: [1, 2, 3, 4]
12+
Output: [24, 12, 8, 6]
1513
1614
=========================================
1715
2 iterations, one from front and the second from back.
@@ -58,4 +56,4 @@ def product_except_self(nums):
5856

5957
# Test 1
6058
# Correct result => [24, 12, 8, 6]
61-
print(product_except_self([1,2,3,4]))
59+
print(product_except_self([1, 2, 3, 4]))

Math/total_divisible_numbers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def total_divisible_numbers(arr, S):
3131

3232
def gcd(a, b):
3333
while a != 0:
34-
a, b = b % a, a
34+
a, b = b % a, a # "Pythonic way"
35+
# or temp = a; a = b % a; b = temp; in the other languages
3536
return b
3637

3738

Other/nth_fibonacci_number.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def nth_fibonacci_4(n):
121121
dp0, dp1 = 0, 1
122122

123123
for i in range(n):
124-
dp0, dp1 = dp1, dp0 + dp1
124+
dp0, dp1 = dp1, dp0 + dp1 # "Pythonic way"
125+
# or dp1 += dp0; dp0 = dp1 - dp0; in other languages
125126

126127
return dp0
127128

Other/power.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def power(a, b):
1717
if b < 0:
1818
# negative power
19-
return 1/power_recursive(a, -b)
19+
return 1 / power_recursive(a, -b)
2020

2121
return power_recursive(a, b)
2222

0 commit comments

Comments
 (0)