😎풀이

  1. 예약된 일정 목록을 담당할 calendar와 중복 예약된 구간을 기록할 booking 선언
  2. 이미 중복예약된 booking 일정과 중복되는 일정은 3중복 이므로 즉시 false 반환
  3. 그렇지 않은 경우 교집합 되는 구간(더 늦은 시작시간과 더 이른 종료시간) 기록
  4. 캘린더에 현재 일정 기록하고 true 반환
class MyCalendarTwo {
    private calendar: [number, number][]
    private booking: [number, number][]
    constructor() {
        this.calendar = []
        this.booking = []
    }

    book(startTime: number, endTime: number): boolean {
        for(const [st, et] of this.booking) {
            if(startTime < et && st < endTime) return false
        }
        for(const [st, et] of this.calendar) {
            if(startTime < et && st < endTime) {
                const maxSt = Math.max(startTime, st)
                const minEt = Math.min(endTime, et)
                this.booking.push([maxSt, minEt])
            }
        }
        this.calendar.push([startTime, endTime])
        return true
    }
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글