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
번호 분류 제목 글쓴이 날짜 조회 수
77 Leetcode [todo] 687. Longest Univalue Path hooni 2020.05.01 137
76 Programmers team game secret hooni 2020.04.30 0
75 Programmers hashmap secret hooni 2020.04.30 0
74 Programmers fib secret hooni 2020.04.30 0
73 Leetcode 997. Find the Town Judge hooni 2020.05.02 478
72 Leetcode 994. Rotting Oranges file hooni 2020.04.14 109
71 Leetcode 993. Cousins in Binary Tree file hooni 2020.04.30 146
70 Leetcode 973. K Closest Points to Origin hooni 2020.04.15 101
69 Leetcode 946. Validate Stack Sequences hooni 2020.04.08 101
68 Leetcode 937. Reorder Data in Log Files hooni 2020.04.25 107
67 Leetcode 897. Increasing Order Search Tree hooni 2020.05.04 447
66 Leetcode 876. Middle of the Linked List hooni 2020.05.04 426
65 Leetcode 872. Leaf-Similar Trees file hooni 2020.05.04 434
64 Leetcode 852. Peak Index in a Mountain Array hooni 2020.04.28 101
63 Leetcode 844. Backspace String Compare hooni 2020.05.05 451
62 Leetcode 783. Minimum Distance Between BST Nodes hooni 2020.05.01 507
61 Leetcode 763. Partition Labels hooni 2020.04.17 159
60 Leetcode 75. Sort Colors hooni 2020.04.14 95
» Leetcode 731. My Calendar II hooni 2020.04.15 94
58 Leetcode 729. My Calendar I hooni 2020.04.15 98
Board Pagination Prev 1 2 3 4 Next
/ 4