Problem

You are given two arrays of strings that represent two inclusive events that happened on the same day , event1 and event2, where:

  • event1 = [startTime1, endTime1] and
  • event2 = [startTime2, endTime2].

Event times are valid 24 hours format in the form of HH:MM.

A conflict happens when two events have some non-empty intersection (i.e., some moment is common to both events).

Return true if there is a conflict between two events. Otherwise, returnfalse.

Examples

Example 1

1
2
3
Input: event1 = ["01:15","02:00"], event2 = ["02:00","03:00"]
Output: true
Explanation: The two events intersect at time 2:00.

Example 2

1
2
3
Input: event1 = ["01:00","02:00"], event2 = ["01:20","03:00"]
Output: true
Explanation: The two events intersect starting from 01:20 to 02:00.

Example 3

1
2
3
Input: event1 = ["10:00","11:00"], event2 = ["14:00","15:00"]
Output: false
Explanation: The two events do not intersect.

Constraints

  • event1.length == event2.length == 2
  • event1[i].length == event2[i].length == 5
  • startTime1 <= endTime1
  • startTime2 <= endTime2
  • All the event times follow the HH:MM format.

Solution

Method 1 – Time Interval Overlap

Intuition

Two events have a conflict if their time intervals overlap. If the start of one event is less than or equal to the end of the other, and vice versa, then they overlap.

Approach

  1. Convert the time strings to minutes for easy comparison.
  2. For two intervals [s1, e1] and [s2, e2], check if max(s1, s2) <= min(e1, e2).
  3. If true, the events overlap; otherwise, they do not.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    bool haveConflict(vector<string>& event1, vector<string>& event2) {
        auto toMin = [](const string& t) {
            return stoi(t.substr(0,2)) * 60 + stoi(t.substr(3,2));
        };
        int s1 = toMin(event1[0]), e1 = toMin(event1[1]);
        int s2 = toMin(event2[0]), e2 = toMin(event2[1]);
        return max(s1, s2) <= min(e1, e2);
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import "strconv"
func toMin(t string) int {
    h, _ := strconv.Atoi(t[:2])
    m, _ := strconv.Atoi(t[3:])
    return h*60 + m
}
func haveConflict(event1, event2 []string) bool {
    s1, e1 := toMin(event1[0]), toMin(event1[1])
    s2, e2 := toMin(event2[0]), toMin(event2[1])
    return max(s1, s2) <= min(e1, e2)
}
func max(a, b int) int { if a > b { return a }; return b }
func min(a, b int) int { if a < b { return a }; return b }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    private int toMin(String t) {
        return Integer.parseInt(t.substring(0,2)) * 60 + Integer.parseInt(t.substring(3,5));
    }
    public boolean haveConflict(String[] event1, String[] event2) {
        int s1 = toMin(event1[0]), e1 = toMin(event1[1]);
        int s2 = toMin(event2[0]), e2 = toMin(event2[1]);
        return Math.max(s1, s2) <= Math.min(e1, e2);
    }
}
1
2
3
4
5
6
7
8
class Solution {
    private fun toMin(t: String) = t.substring(0,2).toInt() * 60 + t.substring(3,5).toInt()
    fun haveConflict(event1: Array<String>, event2: Array<String>): Boolean {
        val s1 = toMin(event1[0]); val e1 = toMin(event1[1])
        val s2 = toMin(event2[0]); val e2 = toMin(event2[1])
        return maxOf(s1, s2) <= minOf(e1, e2)
    }
}
1
2
3
4
5
6
7
class Solution:
    def haveConflict(self, event1: list[str], event2: list[str]) -> bool:
        def to_min(t: str) -> int:
            return int(t[:2]) * 60 + int(t[3:])
        s1, e1 = to_min(event1[0]), to_min(event1[1])
        s2, e2 = to_min(event2[0]), to_min(event2[1])
        return max(s1, s2) <= min(e1, e2)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
impl Solution {
    pub fn have_conflict(event1: Vec<String>, event2: Vec<String>) -> bool {
        fn to_min(t: &str) -> i32 {
            t[0..2].parse::<i32>().unwrap() * 60 + t[3..].parse::<i32>().unwrap()
        }
        let s1 = to_min(&event1[0]); let e1 = to_min(&event1[1]);
        let s2 = to_min(&event2[0]); let e2 = to_min(&event2[1]);
        std::cmp::max(s1, s2) <= std::cmp::min(e1, e2)
    }
}
1
2
3
4
5
6
7
8
class Solution {
    haveConflict(event1: string[], event2: string[]): boolean {
        const toMin = (t: string) => parseInt(t.slice(0,2)) * 60 + parseInt(t.slice(3));
        const s1 = toMin(event1[0]), e1 = toMin(event1[1]);
        const s2 = toMin(event2[0]), e2 = toMin(event2[1]);
        return Math.max(s1, s2) <= Math.min(e1, e2);
    }
}

Complexity

  • ⏰ Time complexity: O(1), as all operations are constant time.
  • 🧺 Space complexity: O(1), as no extra space is used.