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]
andevent2 = [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
HH:MM
format.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
Example 1: event1 = 01:1502:00 event2 = 02:0003:00 Result: T Example 2: event1 = 01:0002:00 event2 = 01:2003:00 Result: F Example 3: event1 = 10:0011:00 event2 = 14:0015:00 Result: F
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
temp.f95:12:30: 12 | conflict = conflictEvents(startTime1, endTime1, startTime2, endTime2) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:24:30: 24 | conflict = conflictEvents(startTime1, endTime1, startTime2, endTime2) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:36:30: 36 | conflict = conflictEvents(startTime1, endTime1, startTime2, endTime2) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:45:88: 45 | function conflictEvents(startTime1, endTime1, startTime2, endTime2) result(conflict) | 1 Error: PROGRAM attribute of ‘conflictevents’ conflicts with PROCEDURE attribute at (1) temp.f95:46:21: 46 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:48:82: 48 | character(len=5), intent(in) :: startTime1, endTime1, startTime2, endTime2 | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:49:27: 49 | logical :: conflict | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:52:73: 52 | integer :: startTime1Int, endTime1Int, startTime2Int, endTime2Int | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:53:41: 53 | read (startTime1,*) startTime1Int | 1 Error: Unexpected READ statement in CONTAINS section at (1) temp.f95:54:37: 54 | read (endTime1,*) endTime1Int | 1 Error: Unexpected READ statement in CONTAINS section at (1) temp.f95:55:41: 55 | read (startTime2,*) startTime2Int | 1 Error: Unexpected READ statement in CONTAINS section at (1) temp.f95:56:37: 56 | read (endTime2,*) endTime2Int | 1 Error: Unexpected READ statement in CONTAINS section at (1) temp.f95:59:83: 59 | if (startTime1Int <= startTime2Int .and. endTime1Int >= startTime2Int) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:60:29: 60 | conflict = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:61:88: 61 | else if (startTime2Int <= startTime1Int .and. endTime2Int >= startTime1Int) then | 1 Error: Unexpected ELSE IF statement in CONTAINS section at (1) temp.f95:62:29: 62 | conflict = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:63:12: 63 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:64:30: 64 | conflict = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:65:11: 65 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:67:7: 67 | end function conflictEvents | 1 Error: Expecting END PROGRAM statement at (1)
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.
#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.