|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +def two_sum1(nums, target): |
| 4 | + """Find indexes i and j of an array whose values add up to the target. |
| 5 | + Can assume that each input has exactly one solution. |
| 6 | +
|
| 7 | + Time: O(n^2) -- nested loops to find all combinations of sums |
| 8 | + Space: O(1) -- loop through the array in place; each sum is O(1) |
| 9 | + """ |
| 10 | + if len(nums) < 2: |
| 11 | + return [None, None] |
| 12 | + |
| 13 | + for i, u in enumerate(nums): |
| 14 | + for j, v in enumerate(nums): |
| 15 | + if u + v == target: |
| 16 | + return [i, j] |
| 17 | + |
| 18 | + return [None, None] |
| 19 | + |
| 20 | + |
| 21 | +def two_sum2(nums, target): |
| 22 | + """Find indexes i and j of an array whose values add up to the target. |
| 23 | + Can assume that each input has exactly one solution. |
| 24 | +
|
| 25 | + Time: O(n) -- loop through the array once; past dict get/set is O(1) |
| 26 | + Space: O(n) -- might have to store all n elements in past dict |
| 27 | + """ |
| 28 | + if len(nums) < 2: |
| 29 | + return [None, None] |
| 30 | + |
| 31 | + past = {} |
| 32 | + for i in range(len(nums)): |
| 33 | + diff = target - nums[i] |
| 34 | + if diff in past: |
| 35 | + return [past[diff], i] |
| 36 | + past[nums[i]] = i |
| 37 | + |
| 38 | + return [None, None] |
| 39 | + |
| 40 | + |
| 41 | +def two_sum3(nums, target): |
| 42 | + """Find indexes i and j of an array whose values add up to the target. |
| 43 | + Can assume that each input has exactly one solution. |
| 44 | +
|
| 45 | + nums *must* be sorted |
| 46 | +
|
| 47 | + Time: O(n) -- might have to traverse to the middle indexes of the array |
| 48 | + Space: O(1) -- traverse the array in place; each sum is O(1) |
| 49 | + """ |
| 50 | + if len(nums) < 2: |
| 51 | + return [None, None] |
| 52 | + |
| 53 | + l, r = 0, len(nums) - 1 |
| 54 | + while nums[l] < nums[r]: |
| 55 | + s = nums[l] + nums[r] |
| 56 | + if s == target: |
| 57 | + return [l, r] |
| 58 | + elif s < target: |
| 59 | + l += 1 |
| 60 | + else: |
| 61 | + r -= 1 |
| 62 | + |
| 63 | + return [None, None] |
| 64 | + |
| 65 | + |
| 66 | +def main(): |
| 67 | + nums = [2, 7, 11, 15] |
| 68 | + target = 9 |
| 69 | + ans = [0, 1] |
| 70 | + |
| 71 | + assert two_sum1(nums, target) == ans |
| 72 | + assert two_sum2(nums, target) == ans |
| 73 | + assert two_sum3(list(nums), target) == ans |
| 74 | + |
| 75 | + |
| 76 | + short_circuit_cases = [ |
| 77 | + ([], 3), # len(nums) == 0 |
| 78 | + ([1], 8), # len(nums) == 1 |
| 79 | + ] |
| 80 | + |
| 81 | + for nums, target in short_circuit_cases: |
| 82 | + assert two_sum1(nums, target) == [None, None] |
| 83 | + assert two_sum2(nums, target) == [None, None] |
| 84 | + assert two_sum3(nums, target) == [None, None] |
| 85 | + |
| 86 | + |
| 87 | + interesting_cases = [ |
| 88 | + ([2, 7, 11, 15], 99, [None, None]), # No matches found |
| 89 | + ] |
| 90 | + |
| 91 | + for nums, target, ans in interesting_cases: |
| 92 | + assert two_sum1(nums, target) == ans |
| 93 | + assert two_sum2(nums, target) == ans |
| 94 | + assert two_sum3(list(nums), target) == ans |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + main() |
0 commit comments