国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

leetcode 2446. Determine if Two Events Have Conflict

這篇具有很好參考價(jià)值的文章主要介紹了leetcode 2446. Determine if Two Events Have Conflict。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

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:(錯(cuò)誤)

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/文章來源地址http://www.zghlxwxcb.cn/news/detail-835168.html

到了這里,關(guān)于leetcode 2446. Determine if Two Events Have Conflict的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 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日
    瀏覽(25)
  • leetcode 445. Add Two Numbers II(兩數(shù)相加)

    leetcode 445. Add Two Numbers II(兩數(shù)相加)

    用鏈表代表2個(gè)數(shù)字,這2個(gè)數(shù)字相加的和用鏈表返回。 最高位在鏈表的head. 思路: 1.鏈表逆序 數(shù)字相加是從低位到高位的,然而鏈表中的數(shù)字是從高位指向低位。 所以涉及到鏈表的逆序。 逆序之后只需從head到tail把兩個(gè)鏈表的數(shù)字相加,再用一個(gè)int表示進(jìn)位。 鏈表的逆序

    2024年02月16日
    瀏覽(26)
  • 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日
    瀏覽(25)
  • 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日
    瀏覽(28)
  • 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日
    瀏覽(14)
  • leetcode - 1751. Maximum Number of Events That Can Be Attended II

    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日
    瀏覽(22)
  • 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日
    瀏覽(23)
  • 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日
    瀏覽(21)
  • 暴力破解(if循環(huán))解決leetcode數(shù)字轉(zhuǎn)成羅馬數(shù)字

    暴力破解(if循環(huán))解決leetcode數(shù)字轉(zhuǎn)成羅馬數(shù)字

    1.題目描述 2.解題思路 剛看到這個(gè)題目的時(shí)候,感覺說的有點(diǎn)啰嗦,其實(shí)不難發(fā)現(xiàn),這個(gè)題目和之前的給你多少錢,什么2元,5元的,給你一個(gè)數(shù)字,讓你算各種錢幣有多少張。無非就是從小到大進(jìn)行判斷,首先判斷給定的數(shù)字,能容納多少個(gè)最大的,然后依次減少。 3.代碼

    2024年02月19日
    瀏覽(20)
  • LeetCode --- 1971. Find if Path Exists in Graph 解題報(bào)告

    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日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包