leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0731.cpp (673B)


      1 class MyCalendarTwo {
      2     using interval = pair<int, int>;
      3     vector<interval> sgl, dbl;
      4 
      5     static bool isOverlap(int s1, int e1, int s2, int e2) { return max(s1, s2) < min(e1, e2); }
      6 
      7     static interval overlap(int s1, int e1, int s2, int e2) { return {max(s1, s2), min(e1, e2)}; }
      8 
      9   public:
     10     bool book(int start, int end) {
     11         for (const auto [s, e] : dbl) {
     12             if (isOverlap(start, end, s, e)) return false;
     13         }
     14 
     15         for (const auto [s, e] : sgl) {
     16             if (!isOverlap(start, end, s, e)) continue;
     17             dbl.push_back(overlap(start, end, s, e));
     18         }
     19 
     20         sgl.emplace_back(start, end);
     21         return true;
     22     }
     23 };