Skip to content

Commit 0b773e0

Browse files
committed
day 2
1 parent a21d80c commit 0b773e0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Two Sum
3+
=======
4+
5+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
6+
7+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
8+
9+
You can return the answer in any order.
10+
11+
Example 1:
12+
Input: nums = [2,7,11,15], target = 9
13+
Output: [0,1]
14+
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
15+
16+
Example 2:
17+
Input: nums = [3,2,4], target = 6
18+
Output: [1,2]
19+
20+
Example 3:
21+
Input: nums = [3,3], target = 6
22+
Output: [0,1]
23+
24+
Constraints:
25+
2 <= nums.length <= 104
26+
-109 <= nums[i] <= 109
27+
-109 <= target <= 109
28+
Only one valid answer exists.
29+
30+
Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
31+
32+
Hint #1
33+
A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.
34+
35+
Hint #2
36+
So, if we fix one of the numbers, say
37+
x
38+
, we have to scan the entire array to find the next number
39+
y
40+
which is
41+
value - x
42+
where value is the input parameter. Can we change our array somehow so that this search becomes faster?
43+
44+
Hint #3
45+
The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
46+
*/
47+
48+
class Solution
49+
{
50+
public:
51+
vector<int> twoSum(vector<int> &nums, int target)
52+
{
53+
unordered_map<int, int> prev;
54+
for (int i = 0; i < nums.size(); ++i)
55+
{
56+
int find = target - nums[i];
57+
if (prev.count(find))
58+
return {prev[find], i};
59+
prev[nums[i]] = i;
60+
}
61+
return {-1, -1};
62+
}
63+
};

Leetcode Daily Challenge/August-2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
| Day | Question Links | Solutions |
44
| :-: | :------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------: |
55
| 1. | [Making A Large Island](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3835/) | [cpp](./01.%20Making%20A%20Large%20Island.cpp) |
6+
| 2. | [Two Sum](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3836/) | [cpp](./02.%20Two%20Sum.cpp) |

0 commit comments

Comments
 (0)