From bf8bd29535fd683e55944761ec9e51b13183dfdb Mon Sep 17 00:00:00 2001 From: Purva Date: Tue, 25 Nov 2025 15:42:16 -0600 Subject: [PATCH] done --- Problem1.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Problem1.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..0c3341ab --- /dev/null +++ b/Problem1.py @@ -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