
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Meeting Scheduler in C++
Suppose we have the availability time slots lists slots1 and slots2 of two people and a meeting duration d, we have to find the earliest time slot that works for both of them and is of duration d. If there is no common time slot that satisfies the requirements, then show an empty array. Here the format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end. we can assume that no two availability slots of the same person intersect with each other. That is, for any two time slots [s1, e1] and [s2, e2] of the same person, either s1 > e2 or s2 > e. So if the input is like s1 = [[10,50], [60,120], [140,210]] and s2 = [[0,15], [60,70]] and duration = 8, then the output will be [60,68].
To solve this, we will follow these steps −
- i := 0 and j := 0, make one array ans, sort s1 and s2.
- while i < size of s1 and j < size of s2
- end := min of s1[i, 1] and s2[j, 1]
- start := min of s1[i, 0] and s2[j, 0]
- if end – start >= duration, then
- insert start and (start + duration) into ans array, and return ans.
- otherwise when s1[i, 1] < s2[j, 1], then increase i by 1
- otherwise increase j by 1
- return ans
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; void print_vector(vector<auto> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } using namespace std; bool cmp(vector <int> a, vector <int> b){ return a[0]<b[0]; } class Solution { public: vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) { int i =0; int j = 0; vector <int> ans; sort(slots1.begin(),slots1.end(),cmp); sort(slots2.begin(),slots2.end(),cmp); while(i<slots1.size() && j<slots2.size()){ int end = min(slots1[i][1],slots2[j][1]); int start = max(slots1[i][0],slots2[j][0]); if(end-start>=duration){ ans.push_back(start); ans.push_back(start+duration); return ans; } else if(slots1[i][1]<slots2[j][1]) { i++; } else { j++;} } return ans; } }; main(){ vector<vector<int>> v = {{10,50},{60,120},{140,210}}; vector<vector<int>> v1 = {{0,15},{60,70}}; Solution ob; print_vector(ob.minAvailableDuration(v, v1, 8)); }
Input
[[10,50],[60,120],[140,210]] [[0,15],[60,70]] 8
Output
[60, 68, ]
- Related Articles
- Scheduler activations
- Event scheduler in Python
- Task Scheduler n C++
- Meeting Rooms II in C++
- Best Meeting Point in C++
- What is a process scheduler in OS?
- Meeting Rooms 2 problem in JavaScript
- How to retrieve tasks in Task scheduler using PowerShell?
- What are a scheduler and a dispatcher in OS?
- How can we start MySQL event scheduler?
- Best meeting point in 2D binary array in C++
- How to use Net Meeting and what is the code to start and join the meeting online?
- Difference Between Long-Term and Short-Term Scheduler in OS
- Difference between Long-Term and Short-Term Scheduler.
- What is dispatcher and difference between dispatcher and scheduler?
