-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_listEqualsNumber.py
65 lines (50 loc) · 1.6 KB
/
1_listEqualsNumber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from bisect import bisect_left
def listEqualNumber(numbers, k):
"""
Returns whether any two numbers from a list add up to k (Brute force)
"""
for i in range(len(numbers)):
for j in range(len(numbers)):
if (i != j and numbers[i] + numbers[j] == k):
return True
return False
numbers = [6, 7, 8, 9]
k = 17
listEqualNumber(numbers, k)
def listEqualNumber2(numbers, k):
"""
Returns whether any two numbers from a list add up to k (Set)
"""
seen = set()
for number in numbers:
if number - k in seen:
return True
seen.add(number)
return False
def listEqualNumber3(numbers, k):
"""
Returns whether any two numbers from a list add up to k (Binary search)
"""
numbers.sort()
for i in range(len(numbers)):
target = k - numbers[i]
j = binarySearch(numbers, target)
# Check that binary search found the target and that it's not in the same index
# as i. If it is in the same index, we can check numbers[i + 1] and numbers[i - 1] to see
# if there's another number that's the same value as numbers[i].
if j == -1:
continue
elif j != i:
return True
elif j + 1 < len(numbers) and numbers[j + 1] == target:
return True
elif j - 1 >= 0 and numbers[j - 1] == target:
return True
return False
def binarySearch(lst, target):
low = 0
high = len(lst)
ind = bisect_left(lst, target, low, high)
if 0 <= ind < high and lst[ind] == target:
return ind
return -1