forked from super30admin/Competitive-Coding-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwosum.py
21 lines (18 loc) · 790 Bytes
/
twosum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""
Space Complexity : O(N) as we have to create a hashmap for all the elements in the array
Time Complexity : O(N) as we have to traverse through the hashmap O(1) for lookup in hashmap
"""
class Solution:
def twoSum(self, nums, target):
#create an empty hashmap
hashmap = {}
for k,v in enumerate(nums): #run through the entire array
b = target - nums[k] #check for the difference between target and every element in k
if b in hashmap:#for every value in hashmap
return [hashmap[b],k] #return the hashmap[key,val]
hashmap[v] = k #else populate the value in hashmap
return
S = Solution()
nums = [2,11,15,7,0,9]
target = 9
print(S.twoSum(nums,target))