Skip to content

Instantly share code, notes, and snippets.

@irvifa
Last active June 17, 2017 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irvifa/fbcd14104168a10320e456652cbab66d to your computer and use it in GitHub Desktop.
Save irvifa/fbcd14104168a10320e456652cbab66d to your computer and use it in GitHub Desktop.
mock myself
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
class Solution {
public:
vector<vi> fourSum(vi &v, int n) {
sort(v.begin(),v.end());
set<vi> s;
int sz, r, lo, hi, i, j;
sz = v.size();
for (i = 0; i <= sz - 4; i++) {
for (j = i + 1; j <= sz - 3; j++) {
r = n - (v[i] +v[j]);
lo = j + 1, hi = sz - 1;
while (lo < hi) {
if (r == v[lo]+v[hi]) {
vector<int> tmp = { v[i], v[j], v[lo], v[hi]};
s.insert(tmp);
lo++, hi--;
} else if (v[lo]+v[hi] < r) {
lo++;
} else if (v[lo]+v[hi] > r) {
hi--;
}
}
}
}
vector<vi> result(s.begin(), s.end());
return result;
}
};
int main() {
vi test = {2, 7, 4, 0, 9, 5, 1, 3};
int n = 20;
Solution s;
vector<vi> afs = s.fourSum(test, n);
for(auto &v: afs) {
for(auto &e: v) {
cout << e << " ";
}
cout << endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
const int N = 2;
class Solution {
public:
vi meetingPlanner(vector<vi> &slotsA, vector<vi> &slotsB, int n) {
// your code goes here
vi ret;
int i, j;
i = j = 0;
//O(M+N)
while(i < slotsA.size() && j < slotsB.size()){
int st = max(slotsA[i][0],slotsB[j][0]);
int fin = min(slotsA[i][1],slotsB[j][1]);
if(st + n <= fin) {
ret.push_back(st);
ret.push_back(st+n);
return ret;
} else if (slotsA[i][0] < slotsB[j][0]){
i++;
} else {
j++;
}
}
return ret;
}
};
int main() {
vector<vi> a = {{10, 50}, {60, 120},{140, 210}};
vector<vi> b = {{0,15},{60,70}};
int dur = 8;
Solution afs;
vector<int> ans = afs.meetingPlanner(a, b, dur);
if(ans.size() == 0) puts("null");
else {
for(int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment