Similar Problems

Similar Problems not available

Determine If Two Events Have Conflict - Leetcode Solution

Companies:

LeetCode:  Determine If Two Events Have Conflict Leetcode Solution

Difficulty: Easy

Topics: string array  

Problem Statement:

Given two events startA and endA and startB and endB, determine if the two events conflict.

Two events conflict if:

  • They overlap in time.
  • They start and end at different times.

Solution:

The problem states that we are given two events with their starting and ending times. We need to determine if these two events conflict with each other.

There are two possible cases where two events can conflict with each other:

  1. They overlap in time: if the start time of one event is before the end time of the other event, then the two events overlap in time. For example, if event A starts at 7 am and ends at 8 am, and event B starts at 7:30 am and ends at 9 am, then the two events overlap from 7:30 am to 8 am.

  2. They start and end at different times: if the end time of one event is before the start time of the other event, then the two events start and end at different times and do not conflict with each other.

To check if the two events conflict with each other, we will compare their start and end times. We will first check if the start time of one event is before the end time of the other event. If this is the case, then the two events overlap in time and we return true. If not, we will check if the end time of one event is before the start time of the other event. If this is the case, then the two events do not overlap and we return false. If neither of these conditions are met, then the two events have the same start and end time, and we consider them to be conflicting with each other.

Pseudo code:

function eventsConflict(startA, endA, startB, endB) { if (startA < endB && startB < endA) { // They overlap in time return true; } else if (endA < startB || endB < startA) { // They start and end at different times return false; } else { // They have the same start and end time return true; } }

This function takes four parameters: startA, endA, startB, and endB, representing the start and end times of the two events. It returns true if the two events conflict with each other, and false otherwise.

Time Complexity:

The time complexity of this algorithm is O(1), as we are doing a constant number of comparisons between the start and end times of the two events.

Determine If Two Events Have Conflict Solution Code

1