Leetcode Solution in Python- 1.“Two Sum” Walkthrough

Sudhan M
4 min readJan 26, 2024

--

Introduction:

In this post, we will delve into three diverse solutions to the Two Sum Problem in Python, thoroughly evaluating their time and space complexity to aid in comprehending the most optimal approach. After the conclusion, a detailed, step-by-step explanation with commented code for each solution is provided to offer deeper insights and clarity. This guide ensures a robust understanding of the varied methods available, allowing for an informed and confident selection based on your specific needs and the nature of the dataset.

Problem Description:

Given an array of integers nums and an integer target, return the indices of the two numbers that add up to the target.

Python Function Signature:

def twoSum(self, nums: List[int], target: int) -> List[int]:

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Solution 1: Brute Force Approach

Explanation:

In this approach, we iterate through the entire list, and for each element, we iterate through the remaining elements to find a pair that sums up to the target.

Python Code:

class Solution(object):
def twoSum(self, nums, target):
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
return []

Solution 2: One-Pass Hash Table

Explanation:

This approach optimizes the Two-Pass Hash Table Approach by only making one iteration through the array.

Python Code:

class Solution(object):
def twoSum(self, nums, target):
numMap = {}
for i, num in enumerate(nums):
complement = target - num
if complement in numMap:
return [numMap[complement], i]
numMap[num] = i
return []

Conclusion and Comparison:

In unraveling the Two Sum problem, three different strategies were extensively examined, each holding its unique advantages and limitations.

  • Brute Force Approach: This method, although straightforward, tends to be inefficient for large datasets due to its O(n²) time complexity. The simplicity of this method might be attractive for smaller datasets, and when the execution time is not a critical factor.
  • One-Pass Hash Table Approach: This is the most time-efficient option among the three, with O(n) time complexity but with a single pass over the dataset. It’s especially advantageous for vast datasets, ensuring both speed and efficiency in finding the two numbers that sum up to the target value.

Final Thoughts

The choice among these methods should be a careful consideration of both the size of the dataset and the permissible trade-offs between time and space complexity.

  • For modest datasets where time complexity is not a pressing issue, the Brute Force Approach might suffice.
  • The One-Pass Hash Table Approach, however, is the ultimate choice for handling larger datasets, where execution time is crucial. It guarantees optimal performance and efficient resource utilization, ensuring the task is accomplished swiftly without unnecessary resource expenditure.

Step-by-Step Explanations with Code Comments:

Solution 1: Brute Force Approach with Comments

class Solution(object):
def twoSum(self, nums, target):
# Step 1: Iterate over the numbers in the array.
for i in range(len(nums)):
# Step 2: For each number, iterate over the rest of the numbers in the array.
for j in range(i+1, len(nums)):
# Step 3: Check if the current two numbers add up to the target.
if nums[i] + nums[j] == target:
return [i, j]
# Step 4: If no such pair is found, return an empty list.
return []
  • Step 1: The outer loop iterates over the numbers in the list.
  • Step 2: The inner loop iterates over the rest of the numbers for each number from the outer loop.
  • Step 3: If a pair sums up to the target, their indices are returned. Step 4: If no such pair is found, an empty list is returned.

Solution 3: One-Pass Hash Table Approach with Comments

class Solution(object):
def twoSum(self, nums, target):
# Step 1: Again, create a dictionary to store numbers and their indices.
numMap = {}
# Step 2: During iteration over the numbers, the complement is calculated for each number.
for i, num in enumerate(nums):
complement = target - num
# Step 3: It checks if the complement exists in the dictionary. If so, the indices are returned.
if complement in numMap:
return [numMap[complement], i]
# Step 4: Otherwise, the current number and its index are added to the dictionary.
numMap[num] = i
# Step 5: If no pair sums up to the target, return an empty list.
return []
  • Step 1: A dictionary is created to store numbers and their indices.
  • Step 2: During iteration over the numbers, the complement is calculated for each number.
  • Step 3: It checks if the complement exists in the dictionary. If so, the indices are returned.
  • Step 4: Otherwise, the current number and its index are added to the dictionary.
  • Step 5: If no pair sums up to the target, an empty list is returned.

↗ To reach out the solution on GitHub

--

--