|
1 | 1 | ---
|
2 | 2 | title: "Refinaid"
|
3 |
| -publishedAt: '2024-06-25' |
| 3 | +publishedAt: 2024-08-27 |
4 | 4 | category: Web Development
|
5 | 5 | tags:
|
6 | 6 | - Go
|
7 | 7 | - Python
|
8 | 8 | - Leetcode
|
9 | 9 | - Array
|
10 | 10 | - Hash Table
|
11 |
| -summary: 'Given an array of integers `nums` and an integer `target`, *return indices of the two numbers such that they add up to `target`*.' |
| 11 | +summary: |
12 | 12 | banner: /images/projects/refinaid.png
|
13 |
| -alt: "LeetCode 0001. Two Sum - Hash Map Solution | Go, Python, C++" |
14 |
| -mathjax: true |
| 13 | +alt: |
15 | 14 | ---
|
16 | 15 |
|
17 |
| -Link 👉🏻 [1. Two Sum](https://leetcode.com/problems/two-sum/) |
18 |
| - |
19 |
| - |
20 |
| - |
21 |
| -### Description |
22 |
| - |
23 |
| - |
24 |
| -Given an array of integers `nums` and an integer `target`, *return indices of the two numbers such that they add up to `target`*. |
25 |
| - |
26 |
| -You may assume that each input would have exactly one solution, and you may not use the same element twice. |
27 |
| - |
28 |
| -You can return the answer in any order. |
29 |
| - |
30 |
| -**Example 1:** |
31 |
| -- Input: `nums = [2,7,11,15], target = 9` |
32 |
| -- Output: `[0,1]` |
33 |
| -- Explanation: `Because nums[0] + nums[1] == 9, we return [0, 1].` |
34 |
| - |
35 |
| -**Example 2:** |
36 |
| -- Input: `nums = [3,2,4], target = 6` |
37 |
| -- Output: `[1,2]` |
38 |
| - |
39 |
| -**Example 3:** |
40 |
| -- Input: `nums = [3,3], target = 6` |
41 |
| -- Output: `[0,1]` |
42 |
| - |
43 |
| -**Constraints:** |
44 |
| - |
45 |
| -- <code>2 <= nums.length <= 10<sup>4</sup></code> |
46 |
| -- <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code> |
47 |
| -- <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code> |
48 |
| -- <code>Only one valid answer exists.</code> |
49 |
| - |
50 |
| -**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity? |
51 |
| - |
52 |
| -### Intuition |
53 |
| - |
54 |
| -Use HashMap to keep numbers and their indices we found. |
55 |
| - |
56 |
| - |
57 |
| -> Create a Hash Table in `GO` with `make` function <sup>[Golang Maps](https://www.geeksforgeeks.org/golang-maps/)</sup>, and use `map[key] = value` to set the value of the key. |
58 |
| -> |
59 |
| -> ```go |
60 |
| -> numMap := make(map[int]int) |
61 |
| -> ``` |
62 |
| -
|
63 |
| -> **How to Work with maps?** <sup>[Go maps in action#Working with maps](https://go.dev/blog/maps)</sup> |
64 |
| -> |
65 |
| -> A two-value assignment tests for the existence of a key: |
66 |
| -> |
67 |
| -> ```go |
68 |
| -> i, ok := m["route"] |
69 |
| -> ``` |
70 |
| -> |
71 |
| -> In this statement, the first `value (i)` is assigned the value stored under the key "route". If that key doesn't exist, i is the value type's zero `value (0)`. The second `value (ok)` is a `bool` that is true if the key exists in the map, and false if not. |
72 |
| -> |
73 |
| -> To test for a key without retrieving the value, use an underscore in place of the first value: |
74 |
| -> |
75 |
| -> ```go |
76 |
| -> _, ok := m["route"] |
77 |
| -> ``` |
78 |
| -> |
79 |
| -> To iterate over the contents of a map, use the range keyword: |
80 |
| -> |
81 |
| -> ```go |
82 |
| -> for key, value := range m { |
83 |
| -> fmt.Println("Key:", key, "Value:", value) |
84 |
| -> } |
85 |
| -> ``` |
86 |
| -
|
87 |
| -### Approach |
88 |
| -
|
89 |
| -1. Traverse the `nums` array and store the difference between the `target` and the current `number` as the `key` and the `index` as the `value` in the HashMap. |
90 |
| -2. If the current `number` is already in the HashMap, return the `index` of the current `number` and the `index` stored in the HashMap. |
91 |
| -3. We still need to return an empty array if there is no solution. |
92 |
| -
|
93 |
| -
|
94 |
| -### Complexity |
95 |
| -- Time complexity: $O(n)$ |
96 |
| -
|
97 |
| -- Space complexity: $O(n)$ |
98 |
| -
|
99 |
| -### Code |
100 |
| -
|
101 |
| -```go |
102 |
| -// Go Solution |
103 |
| -func twoSum(nums []int, target int) []int { |
104 |
| - hashMap := make(map[int]int) |
105 |
| -
|
106 |
| - for i, num := range nums { |
107 |
| - if j, ok := hashMap[num]; ok { |
108 |
| - return []int{j, i} |
109 |
| - } |
110 |
| - hashMap[target - num] = i |
111 |
| - } |
112 |
| -
|
113 |
| - return []int{} |
114 |
| -} |
115 |
| -``` |
116 |
| -
|
117 |
| -```python |
118 |
| -# Python Solution |
119 |
| -class Solution: |
120 |
| - def twoSum(self, nums: List[int], target: int) -> List[int]: |
121 |
| - hMap = {} |
122 |
| - |
123 |
| - for i in range(len(nums)): |
124 |
| - if nums[i] in hMap: |
125 |
| - return [hMap[nums[i]], i] |
126 |
| - else: |
127 |
| - hMap[target - nums[i]] = i |
128 |
| - |
129 |
| - return [] |
130 |
| -``` |
131 |
| - |
132 |
| -```cpp |
133 |
| -// C++ Solution |
134 |
| -class Solution { |
135 |
| -public: |
136 |
| - vector<int> twoSum(vector<int>& nums, int target) { |
137 |
| - unordered_map<int, int> hashMap; |
138 |
| - |
139 |
| - for (int i = 0; i < nums.size(); ++i) { |
140 |
| - int num = nums[i]; |
141 |
| - if (hashMap.find(num) != hashMap.end()) { |
142 |
| - return {hashMap[num], i}; |
143 |
| - } |
144 |
| - hashMap[target - num] = i; |
145 |
| - } |
146 |
| - |
147 |
| - return {}; |
148 |
| - } |
149 |
| -}; |
150 |
| -``` |
151 |
| - |
152 |
| - |
0 commit comments