|
| 1 | +# Two Sum |
| 2 | + |
| 3 | +**Link to Problem**: https://leetcode.com/problems/two-sum |
| 4 | + |
| 5 | +## Solutions |
| 6 | + |
| 7 | +- [Elixir](../elixir/lib/solutions/0001_two_sum/two_sum.ex) |
| 8 | +- [Ruby](../ruby/lib/solutions/0001_two_sum/two_sum.rb) |
| 9 | + |
| 10 | +## Description |
| 11 | + |
| 12 | +Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`. |
| 13 | + |
| 14 | +You may assume that each input would have **exactly one solution**, and you may not use the same element twice. |
| 15 | + |
| 16 | +You can return the answer in any order. |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +### Example 1 |
| 21 | + |
| 22 | +``` |
| 23 | +Input: nums = [2,7,11,15], target = 9 |
| 24 | +Output: [0,1] |
| 25 | +Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. |
| 26 | +``` |
| 27 | + |
| 28 | +### Example 2 |
| 29 | + |
| 30 | +``` |
| 31 | +Input: nums = [3,2,4], target = 6 |
| 32 | +Output: [1,2] |
| 33 | +``` |
| 34 | + |
| 35 | +### Example 3 |
| 36 | + |
| 37 | +``` |
| 38 | +Input: nums = [3,3], target = 6 |
| 39 | +Output: [0,1] |
| 40 | +``` |
| 41 | + |
| 42 | +## Solution |
| 43 | + |
| 44 | +This is my first ever LeetCode problem and I usually suck at algorithms, so this took me a long time to get. |
| 45 | + |
| 46 | +For people like me, the first solution I thought of was to iterate through each element in the list, |
| 47 | +compare it with all the other elements in the list, and try to add both elements to see if it matches |
| 48 | +the target. |
| 49 | + |
| 50 | +Unfortunately, while this does work, it is very slow because you have to go through the list multiple |
| 51 | +times in order to get the result. |
| 52 | + |
| 53 | +The optimal solution is to use a Hash Map which makes it possible to get the same answer while only |
| 54 | +going through the list once. |
| 55 | + |
| 56 | +Before we dive into the solution itself, I'd like to discuss something else first. |
| 57 | + |
| 58 | +One thing that I had to realize was that there was another way to figure out what elements it would |
| 59 | +take to reach the target. The obvious solution is to perform addition on all the elements until we |
| 60 | +get a sum that matches the target. |
| 61 | + |
| 62 | +Another way is to do it backwards and perform subtraction instead. We take the current element and |
| 63 | +subtract that from the target. From there, we try to figure out if there is an element that matches |
| 64 | +the difference. |
| 65 | + |
| 66 | +So how do we do that exactly? By creating a new variable and storing all the value and index of |
| 67 | +the current element if it doesn't satisfy the condition to match the target. In this case, we create |
| 68 | +a Hash Map (or Map in Elixir) since it allows us to store both value and index. |
| 69 | + |
| 70 | +In the case of Example 2, it would look like this: |
| 71 | + |
| 72 | +``` |
| 73 | +[3, 2, 4] |
| 74 | + ^ |
| 75 | + | |
| 76 | +we are here |
| 77 | +``` |
| 78 | + |
| 79 | +Since the target is `6`, we will do `6 - 3` which is `3`. What we then do is look at our map |
| 80 | +to see if there is an element that has `3` as the value. |
| 81 | + |
| 82 | +At this point, our map is still empty, so there won't be any matches. The conclusion for this |
| 83 | +element is that we will skip it and go to the next one. However, we will also add this element |
| 84 | +into the map before we move to the next one: |
| 85 | + |
| 86 | +``` |
| 87 | +{3: 0} |
| 88 | +``` |
| 89 | + |
| 90 | +We put the value as the key, so we can use it for lookups and the index as the value, so we can get it later. |
| 91 | + |
| 92 | +After we stored the first element into the map, we will go to the next element: |
| 93 | + |
| 94 | +``` |
| 95 | +[3, 2, 4] |
| 96 | + ^ |
| 97 | + | |
| 98 | +we are here |
| 99 | +``` |
| 100 | + |
| 101 | +At this point, we will try to do `6 - 2` which is `4`. We then look at the map to see if we |
| 102 | +have any matches. |
| 103 | + |
| 104 | +Since we only have `3` inside the map, there won't be any matches. So we repeat what we did |
| 105 | +in the previous element which is to add the current element to the map and move on to the next element: |
| 106 | + |
| 107 | +``` |
| 108 | +{3: 0, 2: 1} |
| 109 | +``` |
| 110 | + |
| 111 | +Afterwards, we go to the last element: |
| 112 | + |
| 113 | +``` |
| 114 | +[3, 2, 4] |
| 115 | + ^ |
| 116 | + | |
| 117 | +we are here |
| 118 | +``` |
| 119 | + |
| 120 | +Obviously, we will try to do `6 - 4` which will result to `2`. If we do `Map.get(map, target - num)`, |
| 121 | +we will get `2` which has the value of `1`. From there, we should now be able to determine that the |
| 122 | +indices `[1, 2]` are the answer. |
0 commit comments