-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.py
More file actions
71 lines (52 loc) · 1.88 KB
/
TwoSum.py
File metadata and controls
71 lines (52 loc) · 1.88 KB
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
66
67
68
69
70
71
# To run the file, simply call "python [Filename].py" by using the "cmd Terminal".
# -------------------------------------------------------------------------------------------------------------------------------
# Two Sum Problem:
# Find two numbers that sum to the target
# May not use the same element twice
# Assume there is exactly one solution
nums = [1, 3, 4, 5, 7]
target = 7
# Example: 3+4 = 7 so we return the indices (1,2)
# (1,3), (1,4), (1,5), ..., (3,4),
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
index = (i, j)
break
# print(f"Indices: ({i}, {j})") # Output: Indices: (1, 2)
# 4 + 3 + 2 + 1 = 4(4+1)/2
# 1 + ... + n = n(n)/2
# O(n^2)
# .5 * n^2
# -------------------------------------------------------------------------------------------------------------------------------
# Optimized Two Sum Solution using a Dictionary
# O(n)
def two_sum(nums, target):
# Create an empty dictionary
d = {}
# Loop through the array
for i, num in enumerate(nums):
# Calculate the complement
# complement = target - num
# Check if the complement of current number is in dictionary
if target - num in d:
# Return the index of the complement and the current index
return (d[target - num], i)
# Add the current number and index to the dictionary
d[num] = i
nums = [1, 3, 4, 5, 7]
target = 11
d = {}
# i = 0
# d = {}
""" is target - num in d""" # is 7 - 1 in d? no
d[1] = 0
# i = 1
# d = {1: 0}
"""is target - num in d""" # is 7 - 3 in d? no
d[3] = 1
# i = 2
# d = {1:0, 3:1}
"""is target - num in d""" # is 7 - 4 in d? yes because d[3] = 1
# indicies = 2, d[3]
# -------------------------------------------------------------------------------------------------------------------------------