-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwo_sum.py
More file actions
36 lines (27 loc) · 1014 Bytes
/
two_sum.py
File metadata and controls
36 lines (27 loc) · 1014 Bytes
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
from typing import Dict, List, Tuple
Array = List[int]
Result = List[Tuple[int, int]]
def brute_force(array: Array, t: int) -> Result:
result = []
for i, e1 in enumerate(array):
for e2 in array[i + 1 :]:
if e1 + e2 == t:
result.append((e1, e2))
return result
def sorted_array(array: Array, t: int) -> Result:
array = sorted(array)
result = []
for i, e1 in enumerate(array):
for e2 in array[i + 1 :]:
if e1 + e2 == t:
result.append((e1, e2))
return result
def create_hash_table_from_array(array: Array) -> Dict[int, bool]:
# python dictionaries are stored by the hash value of the key for fast lookup
hash_table: Dict[int, bool] = {}
for e in array:
hash_table[e] = True
return hash_table
def hash_table_2_sum(array: Array, t: int) -> Result:
hash_table = create_hash_table_from_array(array)
return [(e, t - e) for e in array if hash_table.get(t - e, False) and t - e != e]