Be the first user to complete this post
|
Add to List |
561. Determine if Two Events Have Conflict - Leet Code
You are given two arrays of strings representing two inclusive events that occurred on the same day: event1
and event2
, where:
event1 = [startTime1, endTime1]
event2 = [startTime2, endTime2]
The event times are in the 24-hour format HH:MM
.
A conflict occurs when the two events overlap, meaning they share at least one moment in time.
Return true
if the events conflict; otherwise, return false
.
Example 1:
Input: event1 = ["09:30", "11:00"], event2 = ["10:00", "12:00"] Output: true Explanation: The events overlap from 10:00 to 11:00.
Example 2:
Input: event1 = ["08:00", "09:00"], event2 = ["09:30", "10:30"] Output: false Explanation: The events do not overlap.
Solution:
This solution checks if two events conflict by determining if their time intervals overlap. Event times are in the 24-hour format HH:MM
.
Steps
- Convert time strings into total minutes since midnight using a helper method.
- Represent each event as a range of minutes.
- Compare the time ranges to determine if the events overlap.
Code:
Output:
Example 1: Input: event1 = ["09:30", "11:00"], event2 = ["10:00", "12:00"] Output: True Example 2: Input: event1 = ["08:00", "09:00"], event2 = ["09:30", "10:30"] Output: False
Reference - Leetcode