Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc88482

Browse files
Added solution for the Two-Sum problem
Signed-off-by: Krishna Kishore Shetty <kkshetty.work@pm.me>
1 parent f542fec commit fc88482

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

‎two_sum/Description.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
```

‎two_sum/solution.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
nums_dict = {}
4+
for i, num in enumerate(nums):
5+
nums_dict[num]=i
6+
for i, num in enumerate(nums):
7+
diff = target-num
8+
diff_index = nums_dict.get(diff)
9+
if diff_index is None or diff_index==i:
10+
continue
11+
else:
12+
return [i, diff_index]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /