---
# System prepended metadata

title: 0729. My Calendar I
tags: [Leetcode, TreeMap, Medium]

---

# 0729. My Calendar I
###### tags: `Leetcode` `Medium` `TreeMap`
Link: https://leetcode.com/problems/my-calendar-i/
## 思路 $O(NlogN)$ $O(N)$
用start做key，每次找到离所给区间的end最近的start，如果这个start对应的end不比现在所给区间的start大就可以
## Code
```java=
class MyCalendar {
    TreeMap<Integer, Integer> map;
    
    public MyCalendar() {
        map = new TreeMap<>();    
    }
    
    public boolean book(int start, int end) {
        Integer low = map.lowerKey(end);
        if(low == null || start >= map.get(low)){
            map.put(start, end);
            return true;
        }
        return false;
    }
}
```