Leetcode
2020.04.15 14:11

# 731. My Calendar II

조회 수 94 추천 수 0 댓글 0
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Implement a `MyCalendarTwo` class to store your events. A new event can be added if adding the event will not cause a triple booking.

Your class will have one method, `book(int start, int end)`. Formally, this represents a booking on the half open interval `[start, end)`, the range of real numbers `x` such that `start <= x < end`.

triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.)

For each call to the method `MyCalendar.book`, return `true` if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return `false` and do not add the event to the calendar.

Your class will be called like this: `MyCalendar cal = new MyCalendar();` `MyCalendar.book(start, end)`

Example 1:

```MyCalendar();
MyCalendar.book(10, 20); // returns true
MyCalendar.book(50, 60); // returns true
MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
Explanation:
The first two events can be booked.  The third event can be double booked.
The fourth event (5, 15) can't be booked, because it would result in a triple booking.
The fifth event (5, 10) can be booked, as it does not use time 10 which is already double booked.
The sixth event (25, 55) can be booked, as the time in [25, 40) will be double booked with the third event;
the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
```

Note:

• The number of calls to `MyCalendar.book` per test case will be at most `1000`.
• In calls to `MyCalendar.book(start, end)``start` and `end` are integers in the range `[0, 10^9]`.

```class MyCalendarTwo {
List<int[]> schedules;
List<int[]> duplicate;

public MyCalendarTwo() {
schedules = new ArrayList<>();
duplicate = new ArrayList<>();
}

public boolean book(int start, int end) {
for(int[] item : duplicate){
if(item[1]>start && end>item[0]){
return false;
}
}

for(int[] item : schedules){
if(item[1]>start && end>item[0]){
}
}

return true;
}
}```

?

 제목+내용제목내용댓글이름닉네임아이디태그
List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
17 Leetcode 287. Find the Duplicate Number 2020.04.16 122
16 Leetcode 253. Meeting Rooms II 2020.04.15 98
» Leetcode 731. My Calendar II 2020.04.15 94
14 Leetcode 729. My Calendar I 2020.04.15 98
13 Leetcode 692. Top K Frequent Words 2020.04.15 87
12 Leetcode 973. K Closest Points to Origin 2020.04.15 101
11 Leetcode 75. Sort Colors 2020.04.14 95
10 Leetcode 2. Add Two Numbers 2020.04.14 90
9 Leetcode 23. Merge k Sorted Lists 2020.04.14 98
8 Leetcode 347. Top K Frequent Elements 2020.04.14 96
7 Leetcode 994. Rotting Oranges 2020.04.14 109
6 Leetcode 3. Longest Substring Without Repeating Characters 2020.04.09 107
5 Leetcode 62. Unique Paths 2020.04.09 99
4 Leetcode 946. Validate Stack Sequences 2020.04.08 101
3 Leetcode 114. Flatten Binary Tree to Linked List 2020.04.06 111
2 Leetcode 430. Flatten a Multilevel Doubly Linked List 2020.04.06 98
1 Leetcode 1055. Shortest Way to Form String 2020.04.06 119
Board Pagination Prev 1 2 3 4 Next
/ 4