Meeting Scheduler Solutions in C++
Number 1229
Difficulty Medium
Acceptance 52.8%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/meeting-scheduler//// Author : liuyubobobo/// Time : 2019-10-19#include <iostream>#include <vector>using namespace std;/// Sorting and Two Pointers/// Time Complexity: O(nlogn)/// Space Complexity: O(1)class Solution {public:vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {sort(slots1.begin(), slots1.end(), [](const vector<int>& a, const vector<int>& b){if(a[0] != b[0]) return a[0] < b[0];return a[1] < b[1];});sort(slots2.begin(), slots2.end(), [](const vector<int>& a, const vector<int>& b){if(a[0] != b[0]) return a[0] < b[0];return a[1] < b[1];});int i = 0, j = 0;while(i < slots1.size() && j < slots2.size()){int a = max(slots1[i][0], slots2[j][0]);int b = min(slots1[i][1], slots2[j][1]);if(b > a && b - a >= duration) return {a, a + duration};if(slots1[i][0] < slots2[j][0]) i ++;else if(slots1[i][0] > slots2[j][0]) j ++;else if(slots1[i][1] < slots1[j][1]) i ++;else j ++;}return {};}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/meeting-scheduler//// Author : liuyubobobo/// Time : 2019-10-19#include <iostream>#include <vector>#include <queue>using namespace std;/// Using Priority Queue/// Time Complexity: O(nlogn)/// Space Complexity: O(1)class Solution {public:vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq1;for(const vector<int>& p: slots1)pq1.push(make_pair(p[0], p[1]));priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq2;for(const vector<int>& p: slots2)pq2.push(make_pair(p[0], p[1]));while(!pq1.empty() && !pq2.empty()){pair<int, int> p1 = pq1.top();pair<int, int> p2 = pq2.top();int a = max(p1.first, p2.first);int b = min(p1.second, p2.second);if(a <= b && a + duration <= b) return {a, a + duration};if(p1 < p2) pq1.pop();else pq2.pop();}return {};}};int main() {return 0;}