Determine if Two Events Have Conflict

🏠 ⬅️ ➡️

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, return false.

Example 1:

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

Example 2:

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:

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

Constraints:

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

Note: This problem is from LeetCode.
Compiled
Executed
⚠️ Correct
program main
    implicit none

    character(len=5), dimension(2) :: event1, event2
    logical :: result

    ! Example 1
    event1 = ["01:15 ", "02:00 "]
    event2 = ["02:00 ", "03:00 "]
    write(*,*) 'Example 1:'
    write(*,*) 'event1 = ', event1
    write(*,*) 'event2 = ', event2
    result = hasConflict(event1, event2)
    write(*,*) 'Result: ', result

    ! Example 2
    event1 = ["01:00 ", "02:00 "]
    event2 = ["01:20 ", "03:00 "]
    write(*,*) 'Example 2:'
    write(*,*) 'event1 = ', event1
    write(*,*) 'event2 = ', event2
    result = hasConflict(event1, event2)
    write(*,*) 'Result: ', result

    ! Example 3
    event1 = ["10:00 ", "11:00 "]
    event2 = ["14:00 ", "15:00 "]
    write(*,*) 'Example 3:'
    write(*,*) 'event1 = ', event1
    write(*,*) 'event2 = ', event2
    result = hasConflict(event1, event2)
    write(*,*) 'Result: ', result

contains

    function hasConflict(event1, event2) result(conflict)
        implicit none
        character(len=5), intent(in) :: event1(:), event2(:)
        integer :: startTime1, endTime1, startTime2, endTime2
        logical :: conflict

        read(event1(1), fmt='(I2)') startTime1
        read(event1(2), fmt='(I2)') endTime1
        read(event2(1), fmt='(I2)') startTime2
        read(event2(2), fmt='(I2)') endTime2

        if (startTime1 < startTime2 .and. endTime1 >= startTime2) then
            conflict = .true.
        else if (startTime2 < startTime1 .and. endTime2 >= startTime1) then
            conflict = .true.
        else
            conflict = .false.
        end if

    end function hasConflict

end program main
Compiled
Executed
Correct
program conflictEvents
    implicit none

    character(len=5) :: startTime1, endTime1, startTime2, endTime2
    logical :: conflict

    ! Example 1
    startTime1 = "01:15"
    endTime1 = "02:00"
    startTime2 = "02:00"
    endTime2 = "03:00"
    conflict = conflictEvents(startTime1, endTime1, startTime2, endTime2)
    if (conflict) then
        write (*,*) "Conflict between events 1 and 2"
    else
        write (*,*) "No conflict between events 1 and 2"
    end if

    ! Example 2
    startTime1 = "01:00"
    endTime1 = "02:00"
    startTime2 = "01:20"
    endTime2 = "03:00"
    conflict = conflictEvents(startTime1, endTime1, startTime2, endTime2)
    if (conflict) then
        write (*,*) "Conflict between events 1 and 2"
    else
        write (*,*) "No conflict between events 1 and 2"
    end if

    ! Example 3
    startTime1 = "10:00"
    endTime1 = "11:00"
    startTime2 = "14:00"
    endTime2 = "15:00"
    conflict = conflictEvents(startTime1, endTime1, startTime2, endTime2)
    if (conflict) then
        write (*,*) "Conflict between events 1 and 2"
    else
        write (*,*) "No conflict between events 1 and 2"
    end if

contains

    function conflictEvents(startTime1, endTime1, startTime2, endTime2) result(conflict)
        implicit none

        character(len=5), intent(in) :: startTime1, endTime1, startTime2, endTime2
        logical :: conflict

        ! Convert the time strings to integers
        integer :: startTime1Int, endTime1Int, startTime2Int, endTime2Int
        read (startTime1,*) startTime1Int
        read (endTime1,*) endTime1Int
        read (startTime2,*) startTime2Int
        read (endTime2,*) endTime2Int

        ! Check if the events intersect
        if (startTime1Int <= startTime2Int .and. endTime1Int >= startTime2Int) then
            conflict = .true.
        else if (startTime2Int <= startTime1Int .and. endTime2Int >= startTime1Int) then
            conflict = .true.
        else
            conflict = .false.
        end if

    end function conflictEvents

end program conflictEvents
🌐 Data from online sources
def has_event_conflict(start1: str, end1: str, start2: str, end2: str) -> bool:
    start_hour1, start_min1 = map(int, start1.split(":"))
    start_hour2, start_min2 = map(int, start2.split(":"))
    end_hour1, end_min1 = map(int, end1.split(":"))
    end_hour2, end_min2 = map(int, end2.split(":"))

    total_min1_start = start_hour1 * 60 + start_min1
    total_min1_end = end_hour1 * 60 + end_min1
    total_min2_start = start_hour2 * 60 + start_min2
    total_min2_end = end_hour2 * 60 + end_min2

    return not (total_min1_end <= total_min2_start or total_min1_start >= total_min2_end)

The algorithm first parses the input strings and separates the hour and minute parts of each event. Then, it converts the event times to the total minutes from 00:00 to account for easier comparisons. Finally, it checks if the two events have any intersection by comparing the start and end time of each event in minutes. If there's a conflict, return true; otherwise, return false.

🌐 Data from online sources
#include <string>

bool hasEventConflict(const std::string& start1, const std::string& end1,
                      const std::string& start2, const std::string& end2) {
    int startHour1 = stoi(start1.substr(0, 2));
    int startMin1 = stoi(start1.substr(3, 2));
    int startHour2 = stoi(start2.substr(0, 2));
    int startMin2 = stoi(start2.substr(3, 2));

    int endHour1 = stoi(end1.substr(0, 2));
    int endMin1 = stoi(end1.substr(3, 2));
    int endHour2 = stoi(end2.substr(0, 2));
    int endMin2 = stoi(end2.substr(3, 2));

    int totalMin1Start = startHour1*60 + startMin1;
    int totalMin1End = endHour1*60 + endMin1;
    int totalMin2Start = startHour2*60 + startMin2;
    int totalMin2End = endHour2*60 + endMin2;

    return !(totalMin1End <= totalMin2Start || totalMin1Start >= totalMin2End);
}

The algorithm first parses the input strings and separates the hour and minute parts of each event. Then, it converts the event times to the total minutes from 00:00 to account for easier comparisons. Finally, it checks if the two events have any intersection by comparing the start and end time of each event in minutes. If there's a conflict, return true; otherwise, return false.