Skip to content
Open

done #1146

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
We store the numbers we encounter with its index as the value and number itself as the key and then search for the complement of that number,
if its already seen, return its index along with current index else add it to hmap
TC is O(n) for iterating through all elements and space is o(n) for storing"""


class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hmap = {}

for i in range(len(nums)):
complement = target - nums[i]
if complement in hmap:
return [hmap[complement], i]
else:
hmap[nums[i]] = i