-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_TwoSum.py
94 lines (71 loc) · 3 KB
/
1_TwoSum.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# By Will Shin
#
#
#-------------------------------------------------------------------------------
# LeetCode prompt
#-------------------------------------------------------------------------------
"""
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
"""
#-------------------------------------------------------------------------------
# Approach
#-------------------------------------------------------------------------------
"""
1. I dont want to use the brute-force method, with will run in O(n^2) complexity. This means a nested for-loop is out of the picture
2. I will approach this through the use of a dictionary which will run a look up in O(1) time vs a "is in" operation for an array, which will run in O(n)
3. One edge case that I defintinitely want to handle is when numbers are repeated. This will be taken care of because you are returning indices as well
"""
#-------------------------------------------------------------------------------
# Solution
#-------------------------------------------------------------------------------
def my_twosum(nums, target):
nums_dict = {value: index for index, value in enumerate(nums)}
to_return = []
# the main loop
for i in range(len(nums)):
current_num = nums[i]
diff = target - current_num
# is this the O(1) lookup?
if diff in nums_dict.keys():
# is
if i is nums_dict[diff]:
continue
to_return.append(i)
to_return.append(nums_dict[diff])
break
return sorted(to_return)
#-------------------------------------------------------------------------------
# Main Leetcode Input Driver
#-------------------------------------------------------------------------------
class Solution:
def twoSum(self, nums, target):
return my_twosum(nums, target)
#-------------------------------------------------------------------------------
# Unit Test
#-------------------------------------------------------------------------------
import unittest
class TestSolution(unittest.TestCase):
def test_PromptTest(self):
target = 13
nums = [2, 7, 11, 15]
ans = [0, 2]
self.assertEqual(Solution().twoSum(nums, target), ans)
def test_SecondTest(self):
target = 6
nums = [3, 2, 4]
ans = [1, 2]
self.assertEqual(Solution().twoSum(nums, target), ans)
def test_ThirdTest(self):
target = 6
nums = [3, 3]
ans = [0, 1]
self.assertEqual(Solution().twoSum(nums, target), ans)
unittest.main()