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] andevent2 = [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.
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 == 5startTime1 <= endTime1startTime2 <= endTime2HH:MM format.This approach involves converting the time given in HH:MM format into minutes since the start of the day for easier comparison. By describing times in a single unit, it simplifies determining if time intervals overlap. Once converted, you can easily check for overlap between the two events by seeing if the start time of one event is within the duration of the other event.
This C function converts each event time from HH:MM format into total minutes elapsed since midnight using the toMinutes helper function. Next, it checks if the events overlap by ensuring the end time of one is not less than the start time of the other.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1) since the operations are constant time calculations.
Space Complexity: O(1) as no extra space beyond integer variables is used.
This approach directly compares the start and end times as strings, taking advantage of lexical comparison in HH:MM format, where times can be compared directly as strings. This approach is particularly simple because the format ensures that lexicographical order corresponds to chronological order.
In this C solution, strcmp is used to leverage lexicographical order for comparing the event times. If one event ends before or at the same time another starts, there is no overlap.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1).
Space Complexity: O(1).
| Approach | Complexity |
|---|---|
| Approach 1: Convert Times to Minutes | Time Complexity: O(1) since the operations are constant time calculations. |
| Approach 2: Direct String Comparison | Time Complexity: O(1). |
2446. Determine if Two Events Have Conflict | Leetcode Weekly 316 | LeetCode 2446 • Bro Coders • 1,414 views views
Watch 9 more video solutions →Practice Determine if Two Events Have Conflict with our built-in code editor and test cases.
Practice on FleetCode