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 720. Longest Word in Dictionary hooni 2020.05.07 571
76 Leetcode 697. Degree of an Array hooni 2020.05.05 540
75 Leetcode 783. Minimum Distance Between BST Nodes hooni 2020.05.01 507
74 Leetcode 225. Implement Stack using Queues hooni 2020.05.05 486
73 Leetcode 532. K-diff Pairs in an Array hooni 2020.05.04 485
72 Leetcode 235. Lowest Common Ancestor of a Binary Search Tree file hooni 2020.05.01 484
71 Leetcode 997. Find the Town Judge hooni 2020.05.02 478
70 Leetcode 687. Longest Univalue Path hooni 2020.05.01 474
69 Leetcode 724. Find Pivot Index hooni 2020.05.05 460
68 Leetcode 222. Count Complete Tree Nodes hooni 2020.05.05 457
67 Leetcode 844. Backspace String Compare hooni 2020.05.05 451
66 Leetcode 605. Can Place Flowers hooni 2020.05.05 450
65 Leetcode 897. Increasing Order Search Tree hooni 2020.05.04 447
64 Leetcode 56. Merge Intervals hooni 2020.05.05 440
63 Leetcode 872. Leaf-Similar Trees file hooni 2020.05.04 434
62 Leetcode 628. Maximum Product of Three Numbers hooni 2020.05.05 428
61 Leetcode 876. Middle of the Linked List hooni 2020.05.04 426
60 Leetcode 448. Find All Numbers Disappeared in an Array hooni 2020.05.05 419
59 Leetcode 203. Remove Linked List Elements hooni 2020.05.04 414
58 Leetcode 270. Closest Binary Search Tree Value hooni 2020.05.01 393
Board Pagination Prev 1 2 3 4 Next
/ 4