Determine whether there is a conflict between the two events
Give you two string arrays event1 and event2, representing two closed interval time period events that occur on the same day, where:
event1 = [startTime1, endTime1] and event2 = [startTime2, endTime2]
The time of the event is a valid 24-hour clock and is given in HH:MM format.
When there is a non-empty intersection of two events (i.e., some moments are contained in both events), a conflict is considered to occur.
Return true if there is a conflict between the two events; otherwise, return false.
Example 1:
Input: event1 = ["01:15", "02:00"], event2 = ["02:00", "03:00"]
Output: true
Explanation: Two events intersect at 2:00.
Example 2:
Input: event1 = ["01:00", "02:00"], event2 = ["01:20", "03:00"]
Output: true
Explanation: The intersection of two events starts at 01:20 and ends at 02:00.
Example 3:
Input: event1 = ["10:00", "11:00"], event2 = ["14:00", "15:00"]
Output: false
Explanation: There is no intersection between the two events.
hint:
== == 2.
event1[i].length == event2[i].length == 5
startTime1 <= endTime1
startTime2 <= endTime2
The time of all events is given in HH:MM format
Convert to time comparison
1 Parses the time string into hours and minutes.
2 Check whether the end time of the first event is after the start time of the second event, and also ensure that the start time of the first event is before the end time of the second event.
3 If both of the above conditions are met, then there is an intersection between the two events, that is, there is a conflict.
Here is the code to implement this logic using Golang:
package main import ( "fmt" "time" ) func isEventConflict(event1, event2 []string) bool { layout := "15:04" startTime1, _ := (layout, event1[0]) endTime1, _ := (layout, event1[1]) startTime2, _ := (layout, event2[0]) endTime2, _ := (layout, event2[1]) // Check for conflicts (Equal after Before) if ((endTime2) || (endTime2)) && ((startTime2) || (startTime2)) { return true } return false } func main() { event1 := []string{"01:15", "02:00"} event2 := []string{"02:00", "03:00"} (isEventConflict(event1, event2)) // Output: true event1 = []string{"01:00", "02:00"} event2 = []string{"01:20", "03:00"} (isEventConflict(event1, event2)) // Output: true event1 = []string{"10:00", "11:00"} event2 = []string{"14:00", "15:00"} (isEventConflict(event1, event2)) // Output: false}
Using pure string comparison and mathematical logic operations
When dealing with conflicts in time intervals, in addition to the two methods mentioned above, pure string comparisons and mathematical logic operations can also be used. Here is another method that does not involve time parsing, which directly compares time strings:
package main import ( "fmt" "strings" ) func isEventConflict(event1 []string, event2 []string) bool { startTime1, endTime1 := event1[0], event1[1] startTime2, endTime2 := event2[0], event2[1] // Check whether the end time of the first event is earlier than the start time of the second event, or whether the start time of the first event is later than the end time of the second event if endTime1 < startTime2 || startTime1 > endTime2 { return false } return true } func main() { event1 := []string{"01:15", "02:00"} event2 := []string{"02:00", "03:00"} result := isEventConflict(event1, event2) (result) // Output: true}
This code directly compares the time string without performing time parsing. It judges whether the time interval has intersections through the dictionary order of the strings. This approach works for simple time strings, but is not as reliable as using the time package to handle complex time logic. Based on the example input, this code will output true, indicating that there is a conflict between the two events.
Note that this method assumes that the input time strings are in line with a valid 24-hour time format.
The above is a detailed example of a method for golang to determine whether there is conflict between two events. For more information about golang to determine conflict between two events, please pay attention to my other related articles!