2446. Determine if Two Events Have Conflict
Explanation
To determine if two events have a conflict, we need to check if there is any overlap between the time intervals of the two events. We can do this by comparing the end time of the first event with the start time of the second event, and vice versa. If there is any overlap, then there is a conflict.
Algorithm
- Parse the input strings to extract the start and end times of both events.
- Check if the end time of the first event is greater than or equal to the start time of the second event, and vice versa.
- If either condition is true, there is a conflict. Return true.
- If both conditions are false, there is no conflict. Return false.
Time Complexity
The time complexity of this algorithm is O(1) since we are performing a constant number of operations regardless of the input size.
Space Complexity
The space complexity of this algorithm is O(1) as we are using a constant amount of extra space.
class Solution {
public boolean isEventConflict(String[] event1, String[] event2) {
String startTime1 = event1[0];
String endTime1 = event1[1];
String startTime2 = event2[0];
String endTime2 = event2[1];
return (endTime1.compareTo(startTime2) >= 0) && (endTime2.compareTo(startTime1) >= 0);
}
}
Code Editor (Testing phase)
Improve Your Solution
Use the editor below to refine the provided solution. Select a programming language and try the following:
- Add import statement if required.
- Optimize the code for better time or space complexity.
- Add test cases to validate edge cases and common scenarios.
- Handle error conditions or invalid inputs gracefully.
- Experiment with alternative approaches to deepen your understanding.
Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.