|
| 1 | +Original: https://leetcode.com/problems/two-sum/solutions/5032323/two-pass-python3-solution-beats-70-o-n-space-and-time-complexity/ |
| 2 | + |
| 3 | +# Intuition |
| 4 | + |
| 5 | +Two-pass solution. |
| 6 | + |
| 7 | +We store the elements and their indices in a dictionary(hashmap). |
| 8 | +And then we use the hashmap to find the existence of (target-current number) in the array. If we don't find any, we move on to the next number. |
| 9 | + |
| 10 | +# Approach |
| 11 | + |
| 12 | +### First Pass |
| 13 | + |
| 14 | +We create a hashmap of elements and their indices, where the elements are the keys. |
| 15 | +An O(n) operation, in both time and space. |
| 16 | + |
| 17 | +In case of repeated elements, the index stored will the last occurrence of the element. This won't cause any problems, as the solution will be found - in the second pass - when are at the first occurrence of the element in the array, wherein the hashmap will contain the second/last occurrence of the array. |
| 18 | + |
| 19 | +### Second Pass |
| 20 | +We go through the array, element by element. |
| 21 | + |
| 22 | +For each element(current), we calculate diff=(target-current). Then we check if `diff` is present in the array, using the hashmap/dictionary's `get()` method. |
| 23 | + |
| 24 | +There are two scenarios where we move on to the next iteration. |
| 25 | + |
| 26 | +- If it is not present, then we get `None` as the result of the `get()` method, so we move on the next iteration. |
| 27 | +- If the diff is found, but it is the same element(current index), then we continue, as the problem description does not allow this. |
| 28 | + |
| 29 | +Now, if we do find the the index of `diff` in the the array, and it is not the current index, then we return both values in a list, solving the problem |
| 30 | + |
| 31 | +### Edge Case |
| 32 | +An input like `[3,3]`, with a target of 6. |
| 33 | + |
| 34 | +After first pass, the dictionary will contain `[3: 1]` as its contents, aka the last occurrence of the element. |
| 35 | + |
| 36 | +In the second pass, when we encounter the 3 at index 0, the dictionary's get will returnn 1 as the index of the `diff`. As this solution does not conflict with the problem description, we can submit the solution as `[0,1]`. |
| 37 | + |
| 38 | +# Complexity |
| 39 | + |
| 40 | +- Time complexity: **O(n)** |
| 41 | + |
| 42 | + Creation of the hashmap of elements and their indices is O(n). Going through the array to find a solution is also O(n), since searching for the existence of a number in the array is a O(1) operation. Therefore overall complexity is O(n). |
| 43 | + |
| 44 | +- Space complexity: **O(n)** |
| 45 | + |
| 46 | + The size of the hashmap grows linearly with the size of the input array. |
| 47 | + |
| 48 | +# Code |
| 49 | + |
| 50 | +```python |
| 51 | +class Solution: |
| 52 | + def twoSum(self, nums: List[int], target: int) -> List[int]: |
| 53 | + nums_dict = {} |
| 54 | + for i, num in enumerate(nums): |
| 55 | + nums_dict[num]=i |
| 56 | + for i, num in enumerate(nums): |
| 57 | + diff = target-num |
| 58 | + diff_index = nums_dict.get(diff) |
| 59 | + if diff_index is None or diff_index==i: |
| 60 | + continue |
| 61 | + else: |
| 62 | + return [i, diff_index] |
| 63 | +``` |
0 commit comments