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]){
                //duplicate.add(new int[]{start>item[0]?start:item[0], end<item[1]?end:item[1]});
                duplicate.add(new int[]{Math.max(start,item[0]), Math.min(end,item[1])});
            }
        }
        
        schedules.add(new int[]{start,end});
        return true;
    }
}

[문제] https://leetcode.com/problems/my-calendar-ii/



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
74 Leetcode 692. Top K Frequent Words hooni 2020.04.15 87
73 Leetcode 2. Add Two Numbers hooni 2020.04.14 90
» Leetcode 731. My Calendar II hooni 2020.04.15 94
71 Leetcode 75. Sort Colors hooni 2020.04.14 95
70 Leetcode 347. Top K Frequent Elements hooni 2020.04.14 96
69 Leetcode 430. Flatten a Multilevel Doubly Linked List file hooni 2020.04.06 98
68 Leetcode 23. Merge k Sorted Lists hooni 2020.04.14 98
67 Leetcode 729. My Calendar I hooni 2020.04.15 98
66 Leetcode 253. Meeting Rooms II hooni 2020.04.15 98
65 Leetcode 20. Valid Parentheses hooni 2020.04.25 98
64 Leetcode 62. Unique Paths file hooni 2020.04.09 99
63 Leetcode 7. Reverse Integer hooni 2020.04.28 100
62 Leetcode 946. Validate Stack Sequences hooni 2020.04.08 101
61 Leetcode 973. K Closest Points to Origin hooni 2020.04.15 101
60 Leetcode 852. Peak Index in a Mountain Array hooni 2020.04.28 101
59 Leetcode 350. Intersection of Two Arrays II hooni 2020.04.28 102
58 Leetcode 443. String Compression hooni 2020.04.25 103
57 Leetcode 38. Count and Say hooni 2020.04.25 103
56 Leetcode 53. Maximum Subarray hooni 2020.04.28 106
55 Leetcode 3. Longest Substring Without Repeating Characters hooni 2020.04.09 107
Board Pagination Prev 1 2 3 4 Next
/ 4