LC001 - TwoSum
Problem
Given an array of numbers and a target integer, return two indices in the array where the values sum up to the target integer.
Assume that there is only one solution in the entire space of the array.
Example
Input: nums = [2,3,7,15], target = 9
Output: [0, 2]
Solution
Naive
The time complexity of the brute force solution is since we have to go through (almost) the entire array for each index that we are checking.
However, the brute force solution does not require any additional memory than is already allocated for the initial array.
Hash Map
The time complexity of the Hash Map solution is just since we only have to go through the entire array once. This is because we save the index and value of the elements in a Python dictionary after we encounter them, so we only have to look up if the key exists and check its value (index) if it does.
However, this solution requires additional memory for the dictionary itself, which is in size.
Last updated