Skip to content

Commit

Permalink
solved: two_sum using a dictionary and algebra complement idea. -@iam…
Browse files Browse the repository at this point in the history
  • Loading branch information
iamserda committed Feb 25, 2025
1 parent 94768a1 commit 1ec9296
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions neetcodeio/algostructybeginners/Lv1-Arrays/two_sum_with_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def two_sum(self, nums: list[int], target: int) -> list[int]:
complements = {}
for index, num in enumerate(nums):
complements[num] = index
complement = target - num
if complement in complements:
return [complements[complement], index]


arr = [3, 4, 5, 6]
target = 7
sol = Solution()
assert sol.two_sum(arr, target) == [0, 1] # efficient solution, O(n) time, O(n) space

0 comments on commit 1ec9296

Please sign in to comment.