chore(Python): add two sum for Dictionaries (#656)
parent
e0194a60d2
commit
ca62f9158c
|
@ -11,6 +11,9 @@
|
||||||
- [Singly](linked_lists/singly.py)
|
- [Singly](linked_lists/singly.py)
|
||||||
- [Reverse List](linked_lists/reverse-linkedlist.py)
|
- [Reverse List](linked_lists/reverse-linkedlist.py)
|
||||||
|
|
||||||
|
## Dictionaries
|
||||||
|
- [Two Sum](dictionaries/two-sum.py)
|
||||||
|
|
||||||
## Multiplication
|
## Multiplication
|
||||||
- [Karatsuba](multiplication/karatsuba.py)
|
- [Karatsuba](multiplication/karatsuba.py)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
"""
|
||||||
|
Problem:
|
||||||
|
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to
|
||||||
|
target. You can not use the same element twice.
|
||||||
|
You can return any one of the answers in any order, if not found return [-1, -1].
|
||||||
|
|
||||||
|
|
||||||
|
Solution:
|
||||||
|
In this solution we check if each element's complement i.e. difference between the target and the element
|
||||||
|
(target−nums[i]) exists in the dictionary. If it does exist, we return current element's index and its complement's
|
||||||
|
index else we store the complement in the dictionary.
|
||||||
|
Beware that the complement must not be nums[i] itself, because we can not use the same element twice.
|
||||||
|
|
||||||
|
Complexity Analysis:
|
||||||
|
Time complexity: O(n). We traverse the list containing nn elements only once. Each lookup in the dictionary costs only
|
||||||
|
O(1) time.
|
||||||
|
Space complexity: O(n). The extra space required depends on the number of items stored in the dictionary,
|
||||||
|
which stores at most nn elements.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def twoSum(nums, target):
|
||||||
|
dictionary = {}
|
||||||
|
# using enumerate to iterate through the list keeping track of both index and element
|
||||||
|
for i, val in enumerate(nums):
|
||||||
|
diff = target - val
|
||||||
|
if diff in dictionary:
|
||||||
|
return [dictionary[diff], i]
|
||||||
|
dictionary[val] = i
|
||||||
|
return [-1, -1]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
arr = list(map(int, input("Enter space-seperated integers: ").split()))
|
||||||
|
tar = int(input("Enter target: "))
|
||||||
|
except ValueError:
|
||||||
|
arr = [2, 7, 11, 15]
|
||||||
|
tar = 9
|
||||||
|
print(twoSum(arr, tar))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue