[LeetCode] Determine if Two Events Have Conflict

준규·2023년 4월 17일
0

1.문제


You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where:

  • event1 = [startTime1, endTime1] and
  • event2 = [startTime2, endTime2].

Event times are valid 24 hours format in the form of HH:MM.

A conflict happens when two events have some non-empty intersection (i.e., some moment is common to both events).

Return true if there is a conflict between two events. Otherwise, return false.


event1 , event2 배열 (HH:MM 형식의 시간 문자열을 요소로 가지는 배열) 이 주어질 때 두 event가 겹치는 시간대가 있으면 true , 아니라면 false를 리턴하면 되는문제이다.


Example 1

Input: event1 = ["01:15","02:00"], event2 = ["02:00","03:00"]
Output: true
Explanation: The two events intersect at time 2:00.

Example 2

Input: event1 = ["01:00","02:00"], event2 = ["01:20","03:00"]
Output: true
Explanation: The two events intersect starting from 01:20 to 02:00.

Example 3

Input: event1 = ["10:00","11:00"], event2 = ["14:00","15:00"]
Output: false
Explanation: The two events do not intersect.

Constraints:

  • evnet1.length == event2.length == 2.
  • event1[i].length == event2[i].length == 5
  • startTime1 <= endTime1
  • startTime2 <= endTime2
  • All the event times follow the HH:MM format.

2.풀이

  1. 두 배열의 start 시간과 end시간이 겹치는 조건인지를 체크한다.

/**
 * @param {string[]} event1
 * @param {string[]} event2
 * @return {boolean}
 */
const haveConflict = function (event1, event2) {
  return (
    (event1[0] <= event2[0] && event2[0] <= event1[1]) ||
    (event1[0] <= event2[1] && event2[1] <= event1[1]) ||
    (event2[0] <= event1[0] && event1[0] <= event2[1]) ||
    (event2[0] <= event1[1] && event1[1] <= event2[1])
  );
};

3.결과

profile
안녕하세요 :)

0개의 댓글