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 ***mon 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:
ev***1.length == event2.length == 2.
event1[i].length == event2[i].length == 5
startTime1 <= endTime1
startTime2 <= endTime2
All the event times follow the HH:MM format.
approch 1
The solution to this problem can be found by checking if the events have any conflicts, in this case, by having the intervals overlap with each other.
The function parameters are two vectors of strings.
The vector of strings represents the time intervals of the event.
The strings represent event times. They are in a valid 24-hour format in the form of HH:MM.
Because the ***parison operator is overloaded for strings, we can directly ***pare the start and end times of the events without parsing strings.
We ***pare the string “01:15” with the string “02:00” by ***paring the ASCII values of each corresponding character in the two strings.
The first case is that if the first event starts before the second event starts and the second event starts before the first event ends, there is a conflict. So, we return true:
if (event1[0] <= event2[0] && event1[1] >= event2[0]) {
return true;
}
The second case is that if the first event starts after the second event starts and the first event ends after the second event ends, there is a conflict. So, we return true:(错误)
if (event2[0] <= event1[0] && event2[1] >= event1[0]) {
return true;
}
The above two cases are the only two cases that can cause a conflict.
If there is no conflict, we return false:
return false;
We can use the following code to implement this approach:
class Solution {
public:
bool haveConflict(vector<string>& event1, vector<string>& event2) {
if (event1[0] <= event2[0] && event1[1] >= event2[0]) {
return true;
}
if (event2[0] <= event1[0] && event2[1] >= event1[0]) {
return true;
}
return false;
}
};
https://www.geeksforgeeks.org/difference-between-argument-and-parameter-in-c-c-with-examples/
approch 2
In this approach, we will convert the time intervals to minutes. Then, we calculate time spans and ***pare them to find a conflict.
Firstly, we can calculate how many minutes have passed from the start of the first event until the end of the second event.
We declare a function named convertTime. This function takes a time string in ‘HH:mm’ format and converts it to the total minutes since midnight. It extracts the hours, multiplies them by 60, and adds the minutes, providing a simple way to represent time in minutes for easier ***parison.
Next, we can calculate the time span of an event by subtracting the end time from the start time.
Time Span = (End Time - Start Time)
Next, we find the overall time range by taking the minimum of the start times and the maximum of the end times. This gives us a span that covers both events.
Total time span = min(Start Time 1, Start Time 2), max(End Time 1, End Time 2)
We declare a variable named x. This variable will store the total time span.
Finally, we check if there is a conflict. If the sum of event1’s time span and event2’s time span is less than x, then there is no conflict. We can use the following code to implement this approach:
#include <string>
class Solution {
public:
bool haveConflict(vector<string>& event1, vector<string>& event2) {
int time1begin = convertTime(event1[0]);
int time1end = convertTime(event1[1]);
int time2begin = convertTime(event2[0]);
int time2end = convertTime(event2[1]);
int mintime = min(time1begin , time2begin);
int maxtime = max(time1end,time2end);
int x = maxtime - mintime;
int intersect = x -(time1end - time1begin) -(time2end-time2begin );
return intersect<0 ||intersect==0 ;
}
int convertTime(string time)
{
int result=0;
result += std::stoi(time.substr(0,2))*60; //
result += std::stoi(time.substr(3,2));
return result;
}
};
https://leetcode.***/problems/determine-if-two-events-have-conflict/