-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2sum.py
More file actions
27 lines (24 loc) · 749 Bytes
/
2sum.py
File metadata and controls
27 lines (24 loc) · 749 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
from typing import List, Tuple
def two_sum(nums: List[int], target: int) -> Tuple[int, int]:
i, j = 0, len(nums) - 1 # O(1)
data = sorted(nums) # O(n*log(n))
while i < j: # O(n)
value = data[i] + data[j] # O(1)
if value > target: # O(1)
j -= 1 # O(1)
if value < target: # O(1)
i += 1 # O(1)
if value == target: # O(1)
break
idx1 = nums.index(data[i]) # n
if data[i] == data[j]: # O(1)
idx2 = nums.index(data[j], idx1+1) # n
else:
idx2 = nums.index(data[j]) # n
return idx1, idx2 # n < n*(log(n)) < n^2
# O(nlog(n))
# O(1)
if __name__ == "__main__":
target = 18
nums = [11, 2, 15, 2, 7]
print(two_sum(nums, target))