LeetCode 题解工作台

判断两个事件是否存在冲突

给你两个字符串数组 event1 和 event2 ,表示发生在同一天的两个闭区间时间段事件,其中: event1 = [startTime 1 , endTime 1 ] 且 event2 = [startTime 2 , endTime 2 ] 事件的时间为有效的 24 小时制且按 HH:MM …

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·string

bolt

答案摘要

如果 的开始时间大于 的结束时间,或者 的结束时间小于 的开始时间,那么两个事件不会有冲突。否则,两个事件存在冲突。 <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2446.Determine%20if%20Two%20Events%20Have%20Confli…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个字符串数组 event1 和 event2 ,表示发生在同一天的两个闭区间时间段事件,其中:

  • event1 = [startTime1, endTime1]
  • event2 = [startTime2, endTime2]

事件的时间为有效的 24 小时制且按 HH:MM 格式给出。

当两个事件存在某个非空的交集时(即,某些时刻是两个事件都包含的),则认为出现 冲突 。

如果两个事件之间存在冲突,返回 true ;否则,返回 false

 

示例 1:

输入:event1 = ["01:15","02:00"], event2 = ["02:00","03:00"]
输出:true
解释:两个事件在 2:00 出现交集。

示例 2:

输入:event1 = ["01:00","02:00"], event2 = ["01:20","03:00"]
输出:true
解释:两个事件的交集从 01:20 开始,到 02:00 结束。

示例 3:

输入:event1 = ["10:00","11:00"], event2 = ["14:00","15:00"]
输出:false
解释:两个事件不存在交集。

 

提示:

  • event1.length == event2.length == 2
  • event1[i].length == event2[i].length == 5
  • startTime1 <= endTime1
  • startTime2 <= endTime2
  • 所有事件的时间都按照 HH:MM 格式给出
lightbulb

解题思路

方法一:字符串比较

如果 event1event1 的开始时间大于 event2event2 的结束时间,或者 event1event1 的结束时间小于 event2event2 的开始时间,那么两个事件不会有冲突。否则,两个事件存在冲突。

时间复杂度 O(1)O(1),空间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
        return not (event1[0] > event2[1] or event1[1] < event2[0])
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    They mention parsing time format into an integer interval first, which points to converting HH:MM into total minutes.

  • question_mark

    They ask whether events that touch at one endpoint should count, which tests whether you noticed the intervals are inclusive.

  • question_mark

    They push on whether parsing is required, which is a hint to discuss why fixed-width HH:MM strings can be compared directly.

warning

常见陷阱

外企场景
  • error

    Using a strict overlap check with < instead of <=, which incorrectly returns false when one event ends exactly when the other starts.

  • error

    Overcomplicating an Easy problem with sorting or minute-by-minute simulation instead of comparing just two intervals.

  • error

    Assuming all string times compare safely without explaining that the HH:MM format is zero-padded and therefore lexicographically ordered.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Return the length of the overlap in minutes instead of a boolean conflict flag.

  • arrow_right_alt

    Given many events on the same day, detect whether any pair conflicts after sorting by start time.

  • arrow_right_alt

    Handle times that can cross midnight, which breaks the simple same-day interval assumption in this problem.

help

常见问题

外企场景

判断两个事件是否存在冲突题解:数组·string | LeetCode #2446 简单