-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairSum.py
35 lines (33 loc) · 1.08 KB
/
pairSum.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
class PairSum:
'''
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
'''
def __init__(self, arr, k):
self.arr = arr
self.k = k
'''
n^2 - algo
'''
def solutionQuadraticTime(self):
for i in range(len(self.arr)-1):
for j in range(i+1, len(self.arr)):
if (self.arr[i] + self.arr[j] == self.k):
return [i, j]
return False
'''
n - algo
'''
def solutionLinearTime(self):
_dict = {}
for i in range(len(self.arr)):
comp = self.k - self.arr[i]
if (comp in _dict):
return [_dict[comp], i]
else:
_dict[self.arr[i]] = i
return False
if __name__ == "__main__":
obj = PairSum([2, 7, 11, 15], 14+8)
#print(obj.solutionQuadraticTime())
print(obj.solutionLinearTime())