-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_sum.py
56 lines (56 loc) · 2.62 KB
/
two_sum.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution:
# This can be done in O(n**2) by just checking every combo of numbers
def twoSum(self, nums: List[int], target: int) -> List[int]:
for a in range(len(nums)):
for b in range(len(nums)):
if a == b:
continue
if nums[a] + nums[b] == target:
return sorted([a, b])
# But there is a better way...
# Originally I figured out that I could track the difference of sums against
# the target number by finding the difference between each pair of sums and
# the target number. This was in the right direction, but because I only
# utilized 3 variables (a, b, c), it would get stuck on a local minimum and
# could not find the true indices for some situations thought it worked for
# most. Unfortunately, "mostly right" isn't good enough in computer science.
# we need to be certain!
def ts(self, nums, target):
a = 0
b = 1
for c in range(2, len(nums)):
# stop when we find our answer
if nums[a] + nums[b] == target:
return [a, b] if a < b else [b, a]
# set a and b to pair of smallest difference to target
ab = abs(target - (nums[a] + nums[b]))
bc = abs(target - (nums[b] + nums[c]))
ac = abs(target - (nums[a] + nums[c]))
minimum = min(ab, bc, ac)
if minimum == ab:
a, b = a, b
if minimum == bc:
a, b = b, c
if minimum == ac:
a, b = a, c
return [a, b] if a < b else [b, a]
# the real solution is to take the previous improvement idea and extend it.
# I forgot what a hashmap was but that is exactly what to use. Elements are
# added to the hashmap after looking at them. At each element, we subtract
# the value of the current element from the target and see if the result is
# in the hashmap. If it is, we return the indices of both elements to
# complete the sum.
#
# Once you realize you can trade linear space to get linear time, you
# realize that you can record 'visited' numbers and thus find the solution
# effectively.
#
# Lesson learned: don't overthing problems, remember basic structures, don't
# be afraid to make tradeoffs if it's fine to do so.
def two_sum(self, nums: List[int], target: int) -> List[int]:
visited = {} # value : index
for index, value in enumerate(nums):
difference = target - value
if difference in visited:
return visited[difference], index
visited[value] = index