My Calendar I Solutions in C++
Number 729
Difficulty Medium
Acceptance 51.9%
Link LeetCode
Other languages Go
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/my-calendar-i/description//// Author : liuyubobobo/// Time : 2017-11-20#include <iostream>#include <vector>using namespace std;/// Brute Force/// Time Complexity: book: O(n)/// total: O(n^2)/// Space Complexity: O(n)class MyCalendar {private:vector<pair<int, int>> calendar;public:MyCalendar() {calendar.clear();}bool book(int start, int end) {pair<int, int> newp = make_pair(start, end);for(pair<int, int> p: calendar)if(overlapped(p, newp))return false;calendar.push_back(newp);return true;}private:// To check whether pa and pb overlapped// we check every points in each pair are within another pairbool overlapped(const pair<int, int>&pa, const pair<int, int>& pb){return ((pa.first >= pb.first) && (pa.first < pb.second)) ||((pa.second > pb.first) && (pa.second <= pb.second)) ||((pb.first >= pa.first) && (pb.first < pa.second)) ||((pb.second > pa.first) && (pb.second <= pa.second));}};void printBool(bool res){cout << (res ? "True" : "False" ) << endl;}int main() {MyCalendar calendar;printBool(calendar.book(10, 20)); // returns trueprintBool(calendar.book(15, 25)); // returns falseprintBool(calendar.book(20, 30)); // returns truereturn 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/my-calendar-i/description//// Author : liuyubobobo/// Time : 2017-11-20#include <iostream>#include <vector>using namespace std;/// Brute Force with easy overlapped check/// Time Complexity: book: O(n)/// total: O(n^2)/// Space Complexity: O(n)class MyCalendar {private:vector<pair<int, int>> calendar;public:MyCalendar() {calendar.clear();}bool book(int start, int end) {pair<int, int> newp = make_pair(start, end);for(pair<int, int> p: calendar)if(overlapped(p, newp))return false;calendar.push_back(newp);return true;}private:// Another easy way to check whether pa and pb overlapped// pa.start < pb.end && pa.end > pb.startbool overlapped(const pair<int, int>&pa, const pair<int, int>& pb){return pa.first < pb.second && pa.second > pb.first;}};void printBool(bool res){cout << (res ? "True" : "False" ) << endl;}int main() {MyCalendar calendar;printBool(calendar.book(10, 20)); // returns trueprintBool(calendar.book(15, 25)); // returns falseprintBool(calendar.book(20, 30)); // returns truereturn 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/my-calendar-i/description//// Author : liuyubobobo/// Time : 2017-11-20#include <iostream>#include <set>using namespace std;/// Using Tree Set with naive scan/// Time Complexity: book: O(n)/// total: O(n^2)/// Space Complexity: O(n)class MyCalendar {private:set<pair<int, int>> calendar;public:MyCalendar() {calendar.clear();}bool book(int start, int end) {pair<int, int> newp = make_pair(start, end);for(pair<int, int> p: calendar)if(overlapped(p, newp))return false;calendar.insert(newp);return true;}private:// Another easy way to check whether pa and pb overlapped// pa.start < pb.end && pa.end > pb.startbool overlapped(const pair<int, int>&pa, const pair<int, int>& pb){return pa.first < pb.second && pa.second > pb.first;}};void printBool(bool res){cout << (res ? "True" : "False" ) << endl;}int main() {MyCalendar calendar;printBool(calendar.book(10, 20)); // returns trueprintBool(calendar.book(15, 25)); // returns falseprintBool(calendar.book(20, 30)); // returns truereturn 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/my-calendar-i/description//// Author : liuyubobobo/// Time : 2017-11-20#include <iostream>#include <set>using namespace std;/// Using Tree Set with logn book/// Time Complexity: book: O(logn)/// total: O(nlogn)/// Space Complexity: O(n)class MyCalendar {private:set<pair<int, int>> calendar;public:MyCalendar() {calendar.clear();}bool book(int start, int end) {pair<int, int> newp = make_pair(start, end);set<pair<int, int>>::iterator iter = calendar.lower_bound(newp);if(iter != calendar.end() && overlapped(*iter, newp))return false;if(iter != calendar.begin()){advance(iter, -1);if(overlapped(*iter, newp))return false;}calendar.insert(newp);return true;}private:// Another easy way to check whether pa and pb overlapped// pa.start < pb.end && pa.end > pb.startbool overlapped(const pair<int, int>&pa, const pair<int, int>& pb){return pa.first < pb.second && pa.second > pb.first;}};void printBool(bool res){cout << (res ? "True" : "False" ) << endl;}int main() {MyCalendar calendar;printBool(calendar.book(10, 20)); // returns trueprintBool(calendar.book(15, 25)); // returns falseprintBool(calendar.book(20, 30)); // returns truereturn 0;}