-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreeSum.cpp
120 lines (116 loc) · 3.33 KB
/
threeSum.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <bits/stdc++.h>
using namespace std;
// class Solution{
// public:
// vector<vector<int>> threeSum(vector<int> arr)
// {
// set<vector<int>> st;
// int n = arr.size();
// for(int i = 0; i< n; i++)
// {
// for(int j = i+1; j < n; j++)
// {
// for(int k = j+1; k < n ;k++)
// {
// if(arr[i]+arr[j]+arr[k] == 0)
// {
// vector<int> temp = {arr[i], arr[j], arr[k]};
// sort(temp.begin(), temp.end());
// st.insert(temp);
// }
// }
// }
// }
// vector<vector<int>> res({st.begin(), st.end()});
// return res;
// }
// };
// this is the bruteforce solution whose time complexity is o(n^3)+o(nlogn)+o(nlogn) and space is o(n).
// class Solution
// {
// public:
// vector<vector<int>> threeSum(vector<int> arr)
// {
// set<vector<int>> st;
// int n = arr.size();
// for(int i = 0; i< n; i++)
// {
// set<int> hash;
// for(int j = i+1; j< n; j++)
// {
// int third = -(arr[i]+arr[j]);
// if(hash.find(third) != hash.end())
// {
// vector<int> temp = {arr[i], arr[j], third};
// sort(temp.begin(), temp.end());
// st.insert(temp);
// }
// hash.insert(arr[j]);
// }
// }
// vector<vector<int>> res(st.begin(), st.end());
// return res;
// }
// };
// this is the better solution whose time complexity is o(n^2)*o(nlogn).
class Solution
{
public:
vector<vector<int>> threeSum(vector<int> arr)
{
vector<vector<int>> ans;
sort(arr.begin(), arr.end());
int n = arr.size();
for(int i = 0; i< n; i++)
{
if(i > 0 && arr[i] == arr[i-1])
{
continue;
}
int j = i+1;
int k = n-1;
while(j < k)
{
long long sum = arr[i]+ arr[j];
sum += arr[k];
if(sum < 0)
{
j++;
}
else if(sum > 0)
{
k--;
}
else{
vector<int> temp = {arr[i], arr[j], arr[k]};
ans.push_back(temp);
j++;
k--;
while(arr[j] == arr[j-1] && j < k)
j++;
while(arr[k] == arr[k+1] && j < k)
k--;
}
}
}
return ans;
}
};
// this is the optimal solution whose time complexity is o(n^n)+o(nlogn) and space complexity is o(n).
int main()
{
vector<int> v = {-1, 0, 1, 2, -1, -4};
Solution s;
vector<vector<int>> a = s.threeSum(v);
int n = a.size();
int m = a[0].size();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}