-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem1.cpp
70 lines (45 loc) · 2.08 KB
/
problem1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
Problem 1:
Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
**/
class Solution {
public:
/**
Solution 1 was my brute force attempt, starting at a point and going through the entire array to see if its partner is there.
If not, move to the next element in the array and repeating the process. If found add both to the vector and return. O(n^2).
**/
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
for(int i = 0; i < nums.size(); i++){
for(int j = i+1; j < nums.size(); j ++){
if(nums[i] + nums[j] == target){
result.push_back(i);
result.push_back(j);
}
}
}
return result;
}
/**
The second solution I tried was using a hashmap. In cpp it is a unordered_map. The reason a hash_map is better is because lookup in a hashmap is done with
hashing, which has O(1) lookup time. A hashing function is used which stores the value in a bucket which can be quickly looked up as the function can
be used on the value you are looking for, which is just one function, so therefore O(1).
You calculate the complement, and if it is found, you can return it. This means, at worst case you will only need one pass so this is O(n).
*/
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
unordered_map<int,int> map;
for(int i = 0; i < nums.size(); i++){
int complement = target - nums[i];
if(!(map.find(complement) == map.end())){
result.push_back(i);
result.push_back(map.at(complement));
return result;
}
map[nums[i]] = i;
}
return result;
}
};