Determine if Two Events Have Conflict - Convert to Minutes [JS]

Description 

Solution: Convert to Minutes & Compare

Convert each time string into minutes.
If end1 >= start1, we know there can be overlap.
However, for the case where event1 is completely out of range of event2, we additionally check that start1 <= end2.

Time Complexity: O(1) 79ms
Space Complexity: O(1) 42.3MB

var haveConflict = function(event1, event2) {
  let start1 = getMinutes(event1[0]), end1 = getMinutes(event1[1]);
  let start2 = getMinutes(event2[0]), end2 = getMinutes(event2[1]);
  return end1 >= start2 && start1 <= end2;
};

function getMinutes(timeStr) {
  let hours = Number(timeStr.slice(0, 2));
  let mins = Number(timeStr.slice(3));
  return hours * 60 + mins;
}

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]