GeezerVison is showing github.com — Added one more solution for find_first_missing_positive.py · kofigo/coding-problems@77c0b0c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Arrays/find_first_missing_positive.py +60-19Lines changed: 60 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | Output: 3 | 14 | 14 |
| 15 | 15 | ========================================= | 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. | 17 | 22 | Time Complexity: O(N) | 18 | 23 | Space Complexity: O(1) | 19 | 24 | ''' | 20 | 25 |
| 21 | 26 |
| 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 | + | 25 | 51 |
| 26 | | -def find_first_missing(a): | | 52 | +############## | | 53 | +# Solution 2 # | | 54 | +############## | | 55 | + | | 56 | +def find_first_missing_2(a): | 27 | 57 | n = len(a) | 28 | 58 |
| 29 | 59 | # eliminate all zeros and all negative numbers | 30 | 60 | 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 | 33 | 63 |
| 34 | 64 | # 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): | 36 | 66 | idx = abs(a[i]) - 1 | 37 | 67 | if idx >= n: | 38 | 68 | continue | 39 | | - val = a[idx] | 40 | 69 |
| 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]) | 44 | 72 |
| 45 | 73 | # find the first non-negative position | 46 | 74 | for i in range(n): | @@ -57,34 +85,47 @@ def find_first_missing(a): | 57 | 85 | # Test 1 | 58 | 86 | # Correct result => 1 | 59 | 87 | 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))) | 61 | 90 |
| 62 | 91 | # Test 2 | 63 | 92 | # Correct result => 2 | 64 | 93 | 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))) | 66 | 96 |
| 67 | 97 | # Test 3 | 68 | 98 | # Correct result => 3 | 69 | 99 | 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))) | 71 | 102 |
| 72 | 103 | # Test 4 | 73 | 104 | # Correct result => 4 | 74 | 105 | 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))) | 76 | 108 |
| 77 | 109 | # Test 5 | 78 | 110 | # Correct result => 5 | 79 | 111 | 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))) | 81 | 114 |
| 82 | 115 | # Test 6 | 83 | 116 | # Correct result => 6 | 84 | 117 | 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))) | 86 | 120 |
| 87 | 121 | # Test 7 | 88 | 122 | # Correct result => 7 | 89 | 123 | 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))) |
Collapse file Copy file name to clipboardExpand all lines: Arrays/find_peak_element.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. | 7 | 7 | You may imagine that nums[-1] = nums[n] = -∞. | 8 | 8 |
| 9 | | -Input: [1,2,3,1] | | 9 | +Input: [1, 2, 3, 1] | 10 | 10 | Output: 2 | 11 | 11 | Output explanation: 3 is a peak element and your function should return the index number 2. | 12 | 12 |
| 13 | | -Input: [1,2,1,3,5,6,4] | | 13 | +Input: [1, 2, 1, 3, 5, 6, 4] | 14 | 14 | Output: 1 or 5 | 15 | 15 | 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. | 16 | 16 |
| @@ -49,8 +49,8 @@ def find_peak_element(nums): | 49 | 49 |
| 50 | 50 | # Test 1 | 51 | 51 | # Correct result => 2 | 52 | | -print(find_peak_element([1,2,3,1])) | | 52 | +print(find_peak_element([1, 2, 3, 1])) | 53 | 53 |
| 54 | 54 | # Test 2 | 55 | 55 | # 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])) |
Collapse file Copy file name to clipboardExpand all lines: Arrays/min_swaps.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,9 +33,10 @@ def min_swaps(a): | 33 | 33 | for i in range(n): | 34 | 34 | # swap the elements till the right element isn't found | 35 | 35 | while a[i] - 1 != i: | | 36 | + swap = a[i] - 1 | 36 | 37 | # 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] | 39 | 40 | a[i] = tmp | 40 | 41 |
| 41 | 42 | swaps += 1 | |
Collapse file Copy file name to clipboardExpand all lines: Arrays/product_of_array_except_self.py +3-5Lines changed: 3 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | Given an array nums of n integers where n > 1, | 5 | 5 | return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. | 6 | | -
| 7 | 6 | Note: Please solve it without division and in O(n). | 8 | | -
| 9 | 7 | Follow up: | 10 | 8 | Could you solve it with constant space complexity? | 11 | 9 | (The output array does not count as extra space for the purpose of space complexity analysis.) | 12 | 10 |
| 13 | | -Input: [1,2,3,4] | 14 | | -Output: [24,12,8,6] | | 11 | +Input: [1, 2, 3, 4] | | 12 | +Output: [24, 12, 8, 6] | 15 | 13 |
| 16 | 14 | ========================================= | 17 | 15 | 2 iterations, one from front and the second from back. | @@ -58,4 +56,4 @@ def product_except_self(nums): | 58 | 56 |
| 59 | 57 | # Test 1 | 60 | 58 | # Correct result => [24, 12, 8, 6] | 61 | | -print(product_except_self([1,2,3,4])) | | 59 | +print(product_except_self([1, 2, 3, 4])) |
Collapse file Copy file name to clipboardExpand all lines: Math/total_divisible_numbers.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,7 +31,8 @@ def total_divisible_numbers(arr, S): | 31 | 31 |
| 32 | 32 | def gcd(a, b): | 33 | 33 | 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 | 35 | 36 | return b | 36 | 37 |
| 37 | 38 |
| |
Collapse file Copy file name to clipboardExpand all lines: Other/nth_fibonacci_number.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -121,7 +121,8 @@ def nth_fibonacci_4(n): | 121 | 121 | dp0, dp1 = 0, 1 | 122 | 122 |
| 123 | 123 | 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 | 125 | 126 |
| 126 | 127 | return dp0 | 127 | 128 |
| |
Collapse file Copy file name to clipboardExpand all lines: Other/power.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | def power(a, b): | 17 | 17 | if b < 0: | 18 | 18 | # negative power | 19 | | - return 1/power_recursive(a, -b) | | 19 | + return 1 / power_recursive(a, -b) | 20 | 20 |
| 21 | 21 | return power_recursive(a, b) | 22 | 22 |
| |
|