|
| 1 | +##################################################################################################################################### |
| 2 | +Problem 1 |
| 3 | +##################################################################################################################################### |
| 4 | +Given a list of numbers and a number k, return whether any two numbers from the list add up to k. |
| 5 | + |
| 6 | +For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. |
| 7 | + |
| 8 | +Bonus: Can you do this in one pass? |
| 9 | + |
| 10 | +brut force solution: O(N^2) |
| 11 | +def two_sum(lst, k): |
| 12 | + for i in range(len(lst)): |
| 13 | + for j in range(len(lst)): |
| 14 | + if i != j and lst[i] + lst[j] == k: |
| 15 | + return True |
| 16 | + return False |
| 17 | + |
| 18 | + OR |
| 19 | + |
| 20 | +Another way is to use a set to remember the numbers we've seen so far. |
| 21 | +Then for a given number, we can check if there is another number that, if added, would sum to k. |
| 22 | +This would be O(N) since lookups of sets are O(1) each. |
| 23 | +def two_sum(lst, k): |
| 24 | + seen = set() |
| 25 | + for num in lst: |
| 26 | + if k - num in seen: |
| 27 | + return True |
| 28 | + seen.add(num) |
| 29 | + return False |
| 30 | + |
| 31 | + OR |
| 32 | +Binary search solution |
| 33 | + |
| 34 | +from bisect import bisect_left |
| 35 | + |
| 36 | + |
| 37 | +def two_sum(lst, K): |
| 38 | + lst.sort() |
| 39 | + |
| 40 | + for i in range(len(lst)): |
| 41 | + target = K - lst[i] |
| 42 | + j = binary_search(lst, target) |
| 43 | + |
| 44 | + # Check that binary search found the target and that it's not in the same index |
| 45 | + # as i. If it is in the same index, we can check lst[i + 1] and lst[i - 1] to see |
| 46 | + # if there's another number that's the same value as lst[i]. |
| 47 | + if j == -1: |
| 48 | + continue |
| 49 | + elif j != i: |
| 50 | + return True |
| 51 | + elif j + 1 < len(lst) and lst[j + 1] == target: |
| 52 | + return True |
| 53 | + elif j - 1 >= 0 and lst[j - 1] == target: |
| 54 | + return True |
| 55 | + return False |
| 56 | + |
| 57 | +def binary_search(lst, target): |
| 58 | + lo = 0 |
| 59 | + hi = len(lst) |
| 60 | + ind = bisect_left(lst, target, lo, hi) |
| 61 | + |
| 62 | + if 0 <= ind < hi and lst[ind] == target: |
| 63 | + return ind |
| 64 | + return -1 |
| 65 | + |
| 66 | +################################################################################################################################### |
| 67 | +#problem 2 |
| 68 | +################################################################################################################################### |
| 69 | +This problem was asked by Uber. |
| 70 | + |
| 71 | +Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. |
| 72 | + |
| 73 | +For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6]. |
| 74 | + |
| 75 | +Follow-up: what if you can't use division? |
| 76 | + |
| 77 | + |
| 78 | +This problem would be easy with division: an optimal solution could just find the product of all numbers in the array and then divide by each of the numbers. |
| 79 | + |
| 80 | +Without division, another approach would be to first see that the ith element simply needs the product of numbers before i and the product of numbers after i. Then we could multiply those two numbers to get our desired product. |
| 81 | + |
| 82 | +In order to find the product of numbers before i, we can generate a list of prefix products. Specifically, the ith element in the list would be a product of all numbers including i. Similarly, we would generate the list of suffix products. |
| 83 | +#This runs in O(N) time and space, since iterating over the input arrays takes O(N) time and creating the prefix and suffix arrays take up O(N) space. |
| 84 | +def products(nums): |
| 85 | + # Generate prefix products |
| 86 | + prefix_products = [] |
| 87 | + for num in nums: |
| 88 | + if prefix_products: |
| 89 | + prefix_products.append(prefix_products[-1] * num) |
| 90 | + else: |
| 91 | + prefix_products.append(num) |
| 92 | + |
| 93 | + # Generate suffix products |
| 94 | + suffix_products = [] |
| 95 | + for num in reversed(nums): |
| 96 | + if suffix_products: |
| 97 | + suffix_products.append(suffix_products[-1] * num) |
| 98 | + else: |
| 99 | + suffix_products.append(num) |
| 100 | + suffix_products = list(reversed(suffix_products)) |
| 101 | + |
| 102 | + # Generate result |
| 103 | + result = [] |
| 104 | + for i in range(len(nums)): |
| 105 | + if i == 0: |
| 106 | + result.append(suffix_products[i + 1]) |
| 107 | + elif i == len(nums) - 1: |
| 108 | + result.append(prefix_products[i - 1]) |
| 109 | + else: |
| 110 | + result.append(prefix_products[i - 1] * suffix_products[i + 1]) |
| 111 | + return result |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
0 commit comments