-
Notifications
You must be signed in to change notification settings - Fork 0
/
4Sum LC-18.cpp
68 lines (63 loc) · 1.95 KB
/
4Sum LC-18.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
// tc=O(NlogN)+O(N^3)
// sc=O(1)
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n=nums.size();
set<vector<int>> res;
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
long tgt=target-((long)nums[i]+(long)nums[j]);
int start=j+1,end=n-1;
while(start<end){
if(tgt==((long)nums[start]+(long)nums[end])){
res.insert({nums[i],nums[j],nums[start],nums[end]});
start++;
end--;
}else if(tgt<((long)nums[start]+(long)nums[end])){
end--;
}else{
start++;
}
}
}
}
return vector<vector<int>>(res.begin(),res.end());
}
};
// tc=O(N^3logN)+nlogn
// SC=o(1)
class Solution {
public:
int binSearch(int start, int end, long target, vector<int>& nums){
while(start<=end){
int mid=start+(end-start)/2;
if(nums[mid]==target){
return mid;
}else if(nums[mid]<target){
start=mid+1;
}else{
end=mid-1;
}
}
return -1;
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n=nums.size();
set<vector<int>> res;
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
long tgt=(long)target-((long)nums[i]+(long)nums[j]+(long)nums[k]);
int l=binSearch(k+1,n-1,tgt, nums);
if(l!=-1){
res.insert({nums[i],nums[j],nums[k],nums[l]});
}
}
}
}
return vector<vector<int>>(res.begin(),res.end());
}
};