Leetcode
2020.04.15 14:10

729. My Calendar I

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

단축키

Prev이전 문서

Next다음 문서

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

단축키

Prev이전 문서

Next다음 문서

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

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

Your class will have the 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`.

double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)

For each call to the method `MyCalendar.book`, return `true` if the event can be added to the calendar successfully without causing a double 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(15, 25); // returns false
MyCalendar.book(20, 30); // returns true
Explanation:
The first event can be booked.  The second can't because time 15 is already booked by another event.
The third event can be booked, as the first event takes every time less than 20, but not including 20.
```

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 MyCalendar {
List<int[]> list;

public MyCalendar() {
list = new ArrayList<>();
}

public boolean book(int start, int end) {
//for(int i=0; i<list.size(); i++){
//    int[] item = list.get(i);
for(int[] item : list){
if(item[1]>start && end>item[0]){
return false;
}
}

list.add(new int[]{start,end});
return true;
}
}

/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(start,end);
*/```

?

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