3169. Count Days Without Meetings #1473
-
Topics: You are given a positive integer Return the count of days when the employee is available for work but no meetings are scheduled. Note: The meetings may overlap. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the number of days an employee is available for work but has no meetings scheduled. The approach involves merging overlapping meetings and calculating the gaps between these merged meetings. Approach
Let's implement this solution in PHP: 3169. Count Days Without Meetings <?php
/**
* @param Integer $days
* @param Integer[][] $meetings
* @return Integer
*/
function countDays($days, $meetings) {
// Sort the meetings by their start time
usort($meetings, function($a, $b) {
return $a[0] - $b[0];
});
// Merge overlapping intervals
$merged = array();
foreach ($meetings as $meeting) {
if (empty($merged)) {
$merged[] = $meeting;
} else {
$lastIdx = count($merged) - 1;
$last = &$merged[$lastIdx];
if ($meeting[0] <= $last[1]) {
// Merge the intervals
$last[1] = max($last[1], $meeting[1]);
} else {
$merged[] = $meeting;
}
}
}
// Calculate the total covered days
$covered = 0;
foreach ($merged as $interval) {
$covered += $interval[1] - $interval[0] + 1;
}
// The result is total days minus covered days
return $days - $covered;
}
// Example test cases
echo countDays(10, [[5,7],[1,3],[9,10]]); // Output: 2
echo "\n";
echo countDays(5, [[2,4],[1,3]]); // Output: 1
echo "\n";
echo countDays(6, [[1,6]]); // Output: 0
?> Explanation:
This approach efficiently handles up to 100,000 meetings and works within the constraints, ensuring optimal performance. |
Beta Was this translation helpful? Give feedback.
We need to determine the number of days an employee is available for work but has no meetings scheduled. The approach involves merging overlapping meetings and calculating the gaps between these merged meetings.
Approach
L…