Skip to content

Commit 13bd021

Browse files
committed
Solved problem 15 : 3Sum
1 parent a41cdaa commit 13bd021

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### Problems Solved
1818

19-
| Total | 57 |
19+
| Total | 58 |
2020
|:---:|:---:|
2121

2222
#### Search By Topic
@@ -40,14 +40,14 @@
4040
| Sliding Window | 2 |
4141
| Stack | 2 |
4242
| Tries | 0 |
43-
| Two Pointers | 5 |
43+
| Two Pointers | 6 |
4444

4545
#### Search By Difficulty
4646

4747
| Difficulty | Number |
4848
|:---|---:|
4949
| Easy | 48 |
50-
| Medium | 9 |
50+
| Medium | 10 |
5151
| Hard | 0 |
5252

5353
## Milestones

Two Pointers/Medium/15_3Sum.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Problem: 15
3+
* Name: 3Sum
4+
* Difficulty: Medium
5+
* Topic: Two Pointers
6+
* Link: https://leetcode.com/problems/3sum
7+
*/
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
// Reduction to Two Sum Sorted Array + Hash Map
13+
// Time Complexity: O(n²)
14+
// Space Complexity: O(n)
15+
vector<vector<int>> threeSum(vector<int>& nums) {
16+
vector<vector<int>> result;
17+
if (nums.size() < 3) {return result;}
18+
sort(nums.begin(), nums.end());
19+
if (nums[0] > 0) {return result;}
20+
unordered_map<int, int> valueIndex;
21+
22+
for (int i = 0; i < nums.size(); i++)
23+
valueIndex[nums[i]] = i;
24+
25+
for (int i = 0; i < nums.size() - 2; i++) {
26+
if (nums[i] > 0){
27+
break;
28+
}
29+
if (i != 0 && nums[i] == nums[i - 1]) {
30+
continue;
31+
}
32+
33+
for (int j = i + 1; j < nums.size() - 1; j++) {
34+
35+
if (j != i + 1 && nums[j] == nums[j - 1]) {
36+
continue;
37+
}
38+
39+
int target = -(nums[i] + nums[j]);
40+
if (valueIndex.find(target) != valueIndex.end() && valueIndex[target] > j){
41+
result.push_back({nums[i], nums[j], target});
42+
}
43+
}
44+
}
45+
return result;
46+
}
47+
48+
// Reduction to Two Sum Sorted Array + Two Pointers
49+
// Time Complexity: O(n²)
50+
// Space Complexity: O(n)
51+
vector<vector<int>> threeSum(vector<int>& nums) {
52+
vector<vector<int>> result;
53+
if (nums.size() < 3) {return result;}
54+
sort(nums.begin(), nums.end());
55+
if (nums[0] > 0) {return result;}
56+
57+
for (int idx = 0; idx < nums.size()-2; idx++){
58+
if (nums[idx] > 0) {
59+
break;
60+
}
61+
if (idx != 0 && nums[idx] == nums[idx-1]){
62+
continue;
63+
}
64+
65+
int l = idx+1, r = int(nums.size())-1;
66+
while (l < r){
67+
int sum = nums[idx] + nums[r] + nums[l];
68+
if (sum < 0){
69+
l++;
70+
}
71+
else if (sum > 0){
72+
r--;
73+
}
74+
else {
75+
result.push_back({nums[idx], nums[l], nums[r]});
76+
while(l < r && nums[l] == nums[l+1]){
77+
l++;
78+
}
79+
l++;
80+
while(l < r && nums[r] == nums[r-1]){
81+
r--;
82+
}
83+
r--;
84+
}
85+
}
86+
}
87+
return result;
88+
}

0 commit comments

Comments
 (0)