[LeetCode] 2446. Determine if Two Events Have Conflict

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.

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

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

event1 = [startTime1, endTime1] 且
event2 = [startTime2, endTime2]
事件的时间为有效的 24 小时制且按 HH:MM 格式给出。

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/determine-if-two-events-have-conflict
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题不涉及算法,但是这道题的做法很巧妙。如果两个 event 的时间冲突,那么一定满足 startTime1 <= endTime2 && endTime1 <= startTime2,画两个平行的线段就知道了,如下图。为什么要考虑 startTime1 <= endTime2 呢,因为有可能 event2 的时间是小于 event1 的。

[startTime1, endTime1]

                        [startTime2, endTime2]

时间O(1)

空间O(1)

Java实现

1 class Solution {
2     public boolean haveConflict(String[] event1, String[] event2) {
3         return event1[0].compareTo(event2[1]) <= 0 && event2[0].compareTo(event1[1]) <= 0;
4     }
5 }

 

LeetCode 题目总结

posted @   CNoodle  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2022-05-17 [LeetCode] 1995. Count Special Quadruplets
2021-05-17 [LeetCode] 1171. Remove Zero Sum Consecutive Nodes from Linked List
2020-05-17 [LeetCode] 1452. People Whose List of Favorite Companies Is Not a Subset of Another List
2020-05-17 [LeetCode] 227. Basic Calculator II
2020-05-17 [LeetCode] 224. Basic Calculator
点击右上角即可分享
微信分享提示