|
| 1 | +from typing import List |
| 2 | + |
| 3 | + |
| 4 | +class Solution: |
| 5 | + def countingSort(self, arr, exp1): |
| 6 | + n = len(arr) |
| 7 | + |
| 8 | + # The output array elements that will have sorted arr |
| 9 | + output = [0] * (n) |
| 10 | + |
| 11 | + # initialize count array as 0 |
| 12 | + count = [0] * (10) |
| 13 | + |
| 14 | + # Store count of occurrences in count[] |
| 15 | + for i in range(0, n): |
| 16 | + index = arr[i] / exp1 |
| 17 | + count[int((index) % 10)] += 1 |
| 18 | + |
| 19 | + # Change count[i] so that count[i] now contains actual |
| 20 | + # position of this digit in output array |
| 21 | + for i in range(1, 10): |
| 22 | + count[i] += count[i - 1] |
| 23 | + |
| 24 | + # Build the output array |
| 25 | + i = n - 1 |
| 26 | + while i >= 0: |
| 27 | + index = arr[i] / exp1 |
| 28 | + output[count[int((index) % 10)] - 1] = arr[i] |
| 29 | + count[int((index) % 10)] -= 1 |
| 30 | + i -= 1 |
| 31 | + |
| 32 | + # Copying the output array to arr[], |
| 33 | + # so that arr now contains sorted numbers |
| 34 | + i = 0 |
| 35 | + for i in range(0, len(arr)): |
| 36 | + arr[i] = output[i] |
| 37 | + |
| 38 | + # Method to do Radix Sort |
| 39 | + def radixSort(self, arr, max1): |
| 40 | + |
| 41 | + # Do counting sort for every digit. Note that instead |
| 42 | + # of passing digit number, exp is passed. exp is 10^i |
| 43 | + # where i is current digit number |
| 44 | + exp = 1 |
| 45 | + while max1 / exp > 0: |
| 46 | + self.countingSort(arr, exp) |
| 47 | + exp *= 10 |
| 48 | + return arr |
| 49 | + |
| 50 | + def firstMissingPositive(self, nums: List[int]) -> int: |
| 51 | + """ |
| 52 | + Find the first missing positive number in O(n) time, meaning that we cycle through the array once and only once, no double fors |
| 53 | +
|
| 54 | + Args: |
| 55 | + nums (List[int]): list of numbers to analyze |
| 56 | +
|
| 57 | + Returns: |
| 58 | + int: the first missing positive |
| 59 | + """ |
| 60 | + altered_nums = set() |
| 61 | + the_sum = 0 |
| 62 | + for each_number in nums: |
| 63 | + if each_number > 0 and each_number not in altered_nums: |
| 64 | + altered_nums.add(each_number) |
| 65 | + the_sum += each_number |
| 66 | + |
| 67 | + arr = self.radixSort(list(altered_nums), max(altered_nums)) |
| 68 | + |
| 69 | + print(arr) |
| 70 | + |
| 71 | + if len(arr) == 0: |
| 72 | + return 1 |
| 73 | + |
| 74 | + min_number = arr[0] |
| 75 | + max_number = arr[-1] |
| 76 | + range_sum = ((min_number + max_number) * (max_number - min_number + 1)) // 2 |
| 77 | + if range_sum == the_sum and min_number == 1: |
| 78 | + return max_number + 1 |
| 79 | + else: |
| 80 | + if min_number == 1: |
| 81 | + ## cycle through, find gap, return |
| 82 | + for i in range(len(arr) - 1): |
| 83 | + if arr[i + 1] - arr[i] > 1: |
| 84 | + return arr[i] + 1 |
| 85 | + return -1 |
| 86 | + return 1 |
| 87 | + |
| 88 | + def firstMissingPositiveV2(self, nums: List[int]) -> int: |
| 89 | + |
| 90 | + n = len(nums) |
| 91 | + r_n = range(n) |
| 92 | + for i in r_n: |
| 93 | + if nums[i] <= 0 or nums[i] > n: |
| 94 | + nums[i] = n + 1 |
| 95 | + |
| 96 | + for i in r_n: |
| 97 | + i_abs = abs(nums[i]) |
| 98 | + |
| 99 | + if i_abs > n: |
| 100 | + continue |
| 101 | + |
| 102 | + i_abs -= 1 |
| 103 | + if nums[i_abs] > 0: |
| 104 | + nums[i_abs] = -1 * nums[i_abs] |
| 105 | + |
| 106 | + for i in r_n: |
| 107 | + if nums[i] >= 0: |
| 108 | + return i + 1 |
| 109 | + |
| 110 | + return n + 1 |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + a = [1, 2, 0] |
| 115 | + b = [3, 4, -1, 1] |
| 116 | + c = [7, 8, 9, 11, 12] |
| 117 | + d = [2] |
| 118 | + e = [1, 1] |
| 119 | + f = [ |
| 120 | + 3, |
| 121 | + 17, |
| 122 | + 7, |
| 123 | + 16, |
| 124 | + 16, |
| 125 | + 8, |
| 126 | + -4, |
| 127 | + 5, |
| 128 | + -4, |
| 129 | + 3, |
| 130 | + -2, |
| 131 | + 18, |
| 132 | + 34, |
| 133 | + 5, |
| 134 | + 1, |
| 135 | + -7, |
| 136 | + 3, |
| 137 | + 3, |
| 138 | + 27, |
| 139 | + 8, |
| 140 | + 23, |
| 141 | + 3, |
| 142 | + -3, |
| 143 | + 2, |
| 144 | + 27, |
| 145 | + 8, |
| 146 | + 15, |
| 147 | + 7, |
| 148 | + -6, |
| 149 | + 15, |
| 150 | + 23, |
| 151 | + -6, |
| 152 | + 3, |
| 153 | + 2, |
| 154 | + 5, |
| 155 | + 23, |
| 156 | + 21, |
| 157 | + 3, |
| 158 | + 2, |
| 159 | + ] |
| 160 | + sol = Solution() |
| 161 | + print(sol.firstMissingPositiveV2(a)) |
| 162 | + print(sol.firstMissingPositiveV2(b)) |
| 163 | + print(sol.firstMissingPositiveV2(c)) |
| 164 | + print(sol.firstMissingPositiveV2(d)) |
| 165 | + print(sol.firstMissingPositiveV2(e)) |
| 166 | + print(sol.firstMissingPositiveV2(f)) |
0 commit comments