← Home

729. My Calendar I

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
data structure designC++Markdown
729

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 729. My Calendar I, the solution in this repository is mainly a data structure design solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: data structure design.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are book.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(NlogN), N operations, each search for O(logN), insert for O(1)
  • Space: O(N)

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 376 ms, faster than 5.28% of C++ online submissions for My Calendar I.
02//Memory Usage: 33.4 MB, less than 100.00% of C++ online submissions for My Calendar I.
03class MyCalendar {
04public:
05    map<int, int> events;
06    
07    MyCalendar() {
08        
09    }
10    
11    bool book(int start, int end) {
12        for(auto it = events.begin(); it != events.end(); it++){
13            if((start >= it->first && start < it->second) || 
14              (end > it->first && end <= it->second) || 
15              (start <= it->first && end >= it->second)){
16                //intersection is not empty
17                return false;
18            }else if(it->first >= end){
19                //no intersection
20                break;
21            }
22        }
23        events[start] = end;
24        return true;
25    }
26};
27
28/**
29 * Your MyCalendar object will be instantiated and called as such:
30 * MyCalendar* obj = new MyCalendar();
31 * bool param_1 = obj->book(start,end);
32 */
33 
34//Approach #2: Balanced Tree, c++ map::upper_bound, map::lower_bound
35//time: O(NlogN), N operations, each search for O(logN), insert for O(1)
36//space: O(N)
37class MyCalendar {
38public:
39    map<int, int> calendar;
40    
41    MyCalendar() {
42        
43    }
44    
45    bool book(int start, int end) {
46        //floorKey
47        auto it = calendar.upper_bound(start);
48        int prev = (it != calendar.begin()) ? (--it)->first : -1;
49        //ceilingKey
50        it = calendar.lower_bound(start);
51        int next = (it != calendar.end()) ? it->first : -1;
52        
53        if((prev == -1 || calendar[prev] <= start) &&
54          (next == -1 || end <= next)){
55            //it's valid iff calendar[prev] <= start <= end <= next
56            calendar[start] = end;
57            return true;
58        }
59        return false;
60    }
61};
62
63/**
64 * Your MyCalendar object will be instantiated and called as such:
65 * MyCalendar* obj = new MyCalendar();
66 * bool param_1 = obj->book(start,end);
67 */

Cost

Complexity

Time
O(NlogN), N operations, each search for O(logN), insert for O(1)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(N)
Auxiliary state plus the answer structure where the problem requires one.