[LeetCode] 2446. Determine if Two Events Have Conflict

Chobby·2025년 10월 30일
1

LeetCode

목록 보기
736/760

😎풀이

  1. 각 시간을 분 단위로 변환
  2. event1이 먼저 시작되는 이벤트라면, event1의 종료 시간보다 event2의 시작 시간이 동일하거나 더 이를 경우 겹침
  3. event2이 먼저 시작되는 이벤트라면, event2의 종료 시간보다 event1의 시작 시간이 동일하거나 더 이를 경우 겹침
  4. 두 경우가 아니라면, 두 이벤트는 겹치지 않음
function haveConflict(event1: string[], event2: string[]): boolean {
    const start1 = timeToMin(event1[0])
    const end1 = timeToMin(event1[1])
    const start2 = timeToMin(event2[0])
    const end2 = timeToMin(event2[1])
    if(start1 <= start2 && end1 >= start2) {
        return true
    }
    if(start2 <= start1 && end2 >= start1) {
        return true
    }
    return false
};

function timeToMin(time: string) {
    const [hour, min] = time.split(":")
    return Number(hour) * 60 + Number(min)
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글