-
Notifications
You must be signed in to change notification settings - Fork 0
/
algos.py
42 lines (39 loc) · 836 Bytes
/
algos.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
A = [-2, 1, 2, 4, 7,11]
target = 13
#TC:O(n^2)
#SC: O(1)
def two_sum_brute_force(A, target):
for i in range(len(A) - 1):
for j in range(i + 1, len(A)):
if A[i] + A[j] == target:
print(A[i], A[j])
return True
return False
#TC: O(n)
#SC: O(n)
def two_sum_hash_table(A, target):
hasht = dict()
for i in range(len(A)):
if A[i] in hasht:
print(hasht[A[i]], A[i])
return True
else:
hasht[target - A[i]] = A[i]
return False
#TO: O(n)
#SC: O(1)
def two_sum(A, target):
i = 0
j = len(A) - 1
while i < j:
if A[i] + A[j] == target:
print(A[i], A[j])
return True
elif A[i] + A[j] < target:
i += 1
else:
j -= 1
return False
print(two_sum(A, target))
#print(two_sum_hash_table(A, target))
#print(two_sum_brute_force(A, target))