#2446
Determine if Two Events Have Conflict
EasyArrayStringHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(1) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(1)Space O(1)
Instead of checking each minute, we can directly compare the start and end times of the events. If the events overlap, it can be determined by checking their start and end times.
⚙️
Algorithm
2 steps- 1Step 1: Convert the start and end times of both events into minutes since midnight.
- 2Step 2: Check if the start time of one event is less than or equal to the end time of the other event and vice versa.
solution.py13 lines
1# Full working Python code
2
3def time_to_minutes(time):
4 hours, minutes = map(int, time.split(':'))
5 return hours * 60 + minutes
6
7def have_conflict(event1, event2):
8 start1 = time_to_minutes(event1[0])
9 end1 = time_to_minutes(event1[1])
10 start2 = time_to_minutes(event2[0])
11 end2 = time_to_minutes(event2[1])
12 return start1 <= end2 and start2 <= end1
13ℹ
Complexity note: This complexity is constant because we perform a fixed number of operations regardless of the input size.
- 1Understanding time intervals is crucial for solving overlap problems.
- 2Direct comparisons of start and end times are often more efficient than iterating through every minute.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.