diff options
Diffstat (limited to '1_array_hashing/two_sum.py')
| -rw-r--r-- | 1_array_hashing/two_sum.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/1_array_hashing/two_sum.py b/1_array_hashing/two_sum.py new file mode 100644 index 0000000..76ea68d --- /dev/null +++ b/1_array_hashing/two_sum.py @@ -0,0 +1,83 @@ +""" +Question + +Two Integer Sum + +Given an array of integers nums and an integer target, return the indices i and j such that nums[i] + nums[j] == target and i != j. + +You may assume that every input has exactly one pair of indices i and j that satisfy the condition. + +Return the answer with the smaller index first. + +Example 1: + +Input: +nums = [3,4,5,6], target = 7 + +Output: [0,1] + +Explanation: nums[0] + nums[1] == 7, so we return [0, 1]. + +Example 2: + +Input: nums = [4,5,6], target = 10 + +Output: [0,2] + +Example 3: + +Input: nums = [5,5], target = 10 + +Output: [0,1] + +Constraints: + + 2 <= nums.length <= 1000 + -10,000,000 <= nums[i] <= 10,000,000 + -10,000,000 <= target <= 10,000,000 +""" + + +from typing import List + + +class Solution: + def hashmap(self, nums: List[int], target: int) -> List[int]: + mp = {} + for i, n in enumerate(nums): + diff = target - n + if diff in mp: + return [mp[diff], i] + mp[nums[i]] = i + return [-1, -1] + + +case1 = [3, 4, 5, 6] +target1 = 7 +case2 = [4, 5, 6] +target2 = 10 +case3 = [5, 5] +target3 = 10 + +solution = Solution() +print(f" hashmap case1: {solution.hashmap(case1, target1)}") +print(f" hashmap case2: {solution.hashmap(case2, target2)}") +print(f" hashmap case3: {solution.hashmap(case3, target3)}") + + +""" +Solution + +url: https://neetcode.io/problems/two-integer-sum +video: https://youtu.be/KLlXCFG5TnA +code: +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + prevMap = {} # val -> index + + for i, n in enumerate(nums): + diff = target - n + if diff in prevMap: + return [prevMap[diff], i] + prevMap[n] = i +""" |
