leetcode 2446. Determine if Two Events Have Conflict

这篇具有很好参考价值的文章主要介绍了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.

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 comparison operator is overloaded for strings, we can directly compare the start and end times of the events without parsing strings.

We compare the string “01:15” with the string “02:00” by comparing 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 compare 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 comparison.

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.com/problems/determine-if-two-events-have-conflict/文章来源地址https://www.toymoban.com/news/detail-835168.html

到了这里,关于leetcode 2446. Determine if Two Events Have Conflict的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • leetcode Median of Two Sorted Arrays

    Given two sorted arrays  nums1  and  nums2  of size  m  and  n  respectively, return  the median  of the two sorted arrays. The overall run time complexity should be  O(log (m+n)) . Example 1: Example 2: Constraints: nums1.length == m nums2.length == n 0 = m = 1000 0 = n = 1000 1 = m + n = 2000 -106 = nums1[i], nums2[i] = 106

    2023年04月08日
    浏览(39)
  • leetcode 445. Add Two Numbers II(两数相加)

    用链表代表2个数字,这2个数字相加的和用链表返回。 最高位在链表的head. 思路: 1.链表逆序 数字相加是从低位到高位的,然而链表中的数字是从高位指向低位。 所以涉及到链表的逆序。 逆序之后只需从head到tail把两个链表的数字相加,再用一个int表示进位。 链表的逆序

    2024年02月16日
    浏览(43)
  • LeetCode //C - 4. Median of Two Sorted Arrays

    Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)) .   Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanati

    2024年02月06日
    浏览(41)
  • LeetCode //167. Two Sum II - Input Array Is Sorted

    Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 = index1 index2 numbers.length. Return the indices of the two numbers, index1 and index2 , added by one as an integer array [index1

    2024年02月15日
    浏览(51)
  • LeetCode 1385. Find the Distance Value Between Two Arrays

    Given two integer arrays  arr1  and  arr2 , and the integer  d ,  return the distance value between the two arrays . The distance value is defined as the number of elements  arr1[i]  such that there is not any element  arr2[j]  where  |arr1[i]-arr2[j]| = d . Example 1: Example 2: Example 3: Constraints: 1 = arr1.length, arr2.length = 500 -1000 = ar

    2024年02月10日
    浏览(42)
  • leetcode - 1751. Maximum Number of Events That Can Be Attended II

    You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend. You can only attend one event at a time. If you choose to attend

    2024年02月16日
    浏览(77)
  • LeetCode //2723. Add Two Promises (Day 30 of LC JavaScript Challenage)

    Given two promises promise1 and promise2 , return a new promise. promise1 and promise2 will both resolve with a number. The returned promise should resolve with the sum of the two numbers.   Example 1: Input: promise1 = new Promise(resolve = setTimeout(() = resolve(2), 20)), promise2 = new Promise(resolve = setTimeout(() = resolve(5), 60)) Output: 7 Explana

    2024年02月11日
    浏览(39)
  • LeetCode //2722. Join Two Arrays by ID (Day 30 of LC JavaScript Challenage)

    Given two arrays arr1 and arr2 , return a new array joinedArray . All the objects in each of the two inputs arrays will contain an id field that has an integer value. joinedArray is an array formed by merging arr1 and arr2 based on their id key. The length of joinedArray should be the length of unique values of id . The returned array should be sorted in asc

    2024年02月11日
    浏览(51)
  • 暴力破解(if循环)解决leetcode数字转成罗马数字

    1.题目描述 2.解题思路 刚看到这个题目的时候,感觉说的有点啰嗦,其实不难发现,这个题目和之前的给你多少钱,什么2元,5元的,给你一个数字,让你算各种钱币有多少张。无非就是从小到大进行判断,首先判断给定的数字,能容纳多少个最大的,然后依次减少。 3.代码

    2024年02月19日
    浏览(40)
  • LeetCode --- 1971. Find if Path Exists in Graph 解题报告

    There is a  bi-directional  graph with  n  vertices, where each vertex is labeled from  0  to  n - 1  ( inclusive ). The edges in the graph are represented as a 2D integer array  edges , where each  edges[i] = [ui, vi]  denotes a bi-directional edge between vertex  ui  and vertex  vi . Every vertex pair is connected by  at most one  edge, and

    2024年02月07日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包