Skip to content
thesarfo

Reference

Determine if Two Events Have Conflict

Checking whether two HH:MM time ranges overlap by converting both to minutes since midnight.

views 0

Two "HH:MM"-"HH:MM" time ranges — do they overlap (including touching at the boundary)? (LeetCode problem)

Approach: convert both events’ times to minutes since midnight, then check the standard interval-overlap condition: start1 <= end2 && start2 <= end1.

class Solution {
public boolean haveConflict(String[] event1, String[] event2) {
int start1 = timeToMinutes(event1[0]);
int end1 = timeToMinutes(event1[1]);
int start2 = timeToMinutes(event2[0]);
int end2 = timeToMinutes(event2[1]);
return start1 <= end2 && start2 <= end1;
}
private int timeToMinutes(String time) {
String[] parts = time.split(":");
int hours = Integer.parseInt(parts[0]);
int minutes = Integer.parseInt(parts[1]);
return hours * 60 + minutes;
}
}

Time: O(1). Space: O(1).