Last active
June 17, 2017 03:49
-
-
Save irvifa/fbcd14104168a10320e456652cbab66d to your computer and use it in GitHub Desktop.
mock myself
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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