-
Notifications
You must be signed in to change notification settings - Fork 253
/
4sum.cpp
119 lines (106 loc) · 4 KB
/
4sum.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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// Bruteforce | O(n^4) time | O(n) space
class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
int n = nums.size();
set<vector<int>> st;
sort(nums.begin(), nums.end());
for (int i =0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
for (int k = j + 1; k < n - 1; k++) {
for (int t = k + 1; t < n; t++) {
if ((ll)nums[i] + nums[j] + nums[k] + nums[t] == (ll)target) {
vector<int> quad = {nums[i], nums[j], nums[k], nums[t]};
st.insert(quad);
}
}
}
}
}
vector<vector<int>> res(st.begin(), st.end());
return res;
}
};
// Bruteforce | O(n^4) time | O(1) space
class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
int n = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i =0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
for (int k = j + 1; k < n - 1; k++) {
for (int t = k + 1; t < n; t++) {
if ((ll)nums[i] + nums[j] + nums[k] + nums[t] == (ll)target) {
vector<int> quad = {nums[i], nums[j], nums[k], nums[t]};
res.push_back(quad);
}
while (t < n - 1 && nums[t] == nums[t + 1]) t++;
}
while (k < n - 1 && nums[k] == nums[k + 1]) k++;
}
while (j < n - 1 && nums[j] == nums[j + 1]) j++;
}
while (i < n - 1 && nums[i] == nums[i + 1]) i++;
}
return res;
}
};
// Optimal | O(n^3 * log(n)) time | O(1) space
class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
int n = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i = 0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
for (int k = j + 1; k < n - 1; k++) {
ll sumOfThree = (ll)nums[i] + (ll)nums[j] + (ll)nums[k];
int fourthNum = target - sumOfThree;
if (binary_search(nums.begin() + k + 1, nums.end(), fourthNum)) {
vector<int> quad = {nums[i], nums[j], nums[k], fourthNum};
res.push_back(quad);
}
while (k < n - 1 && nums[k] == nums[k + 1]) k++;
}
while (j < n - 1 && nums[j] == nums[j + 1]) j++;
}
while (i < n - 1 && nums[i] == nums[i + 1]) i++;
}
return res;
}
};
// Most Optimal | O(n^3) time | O(1) space
class Solution {
public:
vector<vector<int>> fourSum(vector<int> &nums, int target) {
int n = nums.size();
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for (int i = 0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
int l = j + 1, r = n - 1;
ll remSum = (ll)target - (ll)nums[i] - (ll)nums[j];
while (l < r) {
ll sumOfLastTwo = nums[l] + nums[r];
if (sumOfLastTwo < remSum) l++;
else if (sumOfLastTwo > remSum) r--;
else {
vector<int> quad = {nums[i], nums[j], nums[l], nums[r]};
res.push_back(quad);
while (l < r && nums[l] == quad[2]) l++;
while (l < r && nums[r] == quad[3]) r--;
}
}
while (j < n - 1 && nums[j] == nums[j + 1]) j++;
}
while (i < n - 1 && nums[i] == nums[i + 1]) i++;
}
return res;
}
};