---
# System prepended metadata

title: 0732. My Calendar III
tags: [Leetcode, Line Sweep, Hard]

---

# 0732. My Calendar III
###### tags: `Leetcode` `Hard` `Line Sweep`
Link: https://leetcode.com/problems/my-calendar-iii/
## 思路
和[2251. Number of Flowers in Full Bloom](https://hackmd.io/SlEo8THMSG2IQFjEy0qXKQ)差不多
差分法 Line Sweep
## Code
```java=
class MyCalendarThree {
    TreeMap<Integer, Integer> timeline;
    public MyCalendarThree() {
        timeline = new TreeMap<>();
    }
    
    public int book(int start, int end) {
        timeline.put(start, timeline.getOrDefault(start,0)+1);
        timeline.put(end, timeline.getOrDefault(end,0)-1);
        int ans = 0, sum = 0;
        for(int v:timeline.values()){
            ans = Math.max(ans, sum+=v);
        }
        return ans;
    }
}
```