Leetcode 2446. Determine if Two Events Have Conflict
You are given two arrays of strings event1
and event2
where:
event1 = [startTime1, endTime1]
andevent2 = [startTime2, endTime2]
.Event times are given in the format “HH:MM”.
Determine if the two events have a conflict, meaning they overlap in time.
Return true
if there is a conflict between the two events, and false
otherwise.
[startTime1, endTime1]
and [startTime2, endTime2]
overlap.
[start1, end1]
and [start2, end2]
overlap if start1 < end2
and start2 < end1
.public class EventConflict {
public boolean haveConflict(String[] event1, String[] event2) {
int start1 = toMinutes(event1[0]);
int end1 = toMinutes(event1[1]);
int start2 = toMinutes(event2[0]);
int end2 = toMinutes(event2[1]);
// Check if there is an overlap
return start1 < end2 && start2 < end1;
}
private int toMinutes(String time) {
String[] parts = time.split(":");
int hours = Integer.parseInt(parts[0]);
int minutes = Integer.parseInt(parts[1]);
return hours * 60 + minutes;
}
public static void main(String[] args) {
EventConflict ec = new EventConflict();
String[] event1 = {"01:15", "02:00"};
String[] event2 = {"02:00", "03:00"};
System.out.println(ec.haveConflict(event1, event2)); // Output should be true
}
}
toMinutes
converts “HH:MM” time into minutes since midnight.The time complexity is O(1) because it involves only a constant amount of work (parsing a few strings and basic comparisons).
The space complexity is also O(1) because we only use a fixed amount of additional memory regardless of the input size.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?