返回介绍

solution / 2400-2499 / 2446.Determine if Two Events Have Conflict / README_EN

发布于 2024-06-17 01:03:05 字数 4166 浏览 0 评论 0 收藏 0

2446. Determine if Two Events Have Conflict

中文文档

Description

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.

Solutions

Solution 1: String Comparison

If the start time of $event1$ is later than the end time of $event2$, or the end time of $event1$ is earlier than the start time of $event2$, then the two events will not conflict. Otherwise, the two events will conflict.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
    return not (event1[0] > event2[1] or event1[1] < event2[0])
class Solution {
  public boolean haveConflict(String[] event1, String[] event2) {
    return !(event1[0].compareTo(event2[1]) > 0 || event1[1].compareTo(event2[0]) < 0);
  }
}
class Solution {
public:
  bool haveConflict(vector<string>& event1, vector<string>& event2) {
    return !(event1[0] > event2[1] || event1[1] < event2[0]);
  }
};
func haveConflict(event1 []string, event2 []string) bool {
  return !(event1[0] > event2[1] || event1[1] < event2[0])
}
function haveConflict(event1: string[], event2: string[]): boolean {
  return !(event1[0] > event2[1] || event1[1] < event2[0]);
}
impl Solution {
  pub fn have_conflict(event1: Vec<String>, event2: Vec<String>) -> bool {
    !(event1[1] < event2[0] || event1[0] > event2[1])
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文