Determine if Two Events Have Conflict Problem
#2446
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 2446.
You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where:
event1 = [startTime_1, endTime_1]andevent2 = [startTime_2, endTime_2].
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.
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:
event1.length == event2.length == 2event1[i].length == event2[i].length == 5startTime_1 <= endTime_1startTime_2 <= endTime_2- All the event times follow the
HH:MMformat.
Solution
Approach: String Comparison
Times in HH:MM format compare correctly as strings. The events do not conflict only if one ends before the other starts: event1 starts after event2 ends, or event1 ends before event2 starts. In every other case they overlap.
Complexity: O(1) time and O(1) space.
class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
return not (event1[0] > event2[1] or event1[1] < event2[0])