3439. Reschedule Meetings for Maximum Free Time I #1906
-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to maximize the longest continuous period of free time during an event by rescheduling at most Approach
Let's implement this solution in PHP: 3439. Reschedule Meetings for Maximum Free Time I <?php
/**
* @param Integer $eventTime
* @param Integer $k
* @param Integer[] $startTime
* @param Integer[] $endTime
* @return Integer
*/
function maxFreeTime($eventTime, $k, $startTime, $endTime) {
$n = count($startTime);
$gaps = array();
$gaps[] = $startTime[0];
for ($i = 1; $i < $n; $i++) {
$gaps[] = $startTime[$i] - $endTime[$i-1];
}
$gaps[] = $eventTime - $endTime[$n-1];
$L = min($k + 1, $n + 1);
if ($L == 0) {
return 0;
}
$current_sum = 0;
for ($i = 0; $i < $L; $i++) {
$current_sum += $gaps[$i];
}
$max_sum = $current_sum;
$total_gaps = $n + 1;
for ($i = $L; $i < $total_gaps; $i++) {
$current_sum = $current_sum - $gaps[$i - $L] + $gaps[$i];
if ($current_sum > $max_sum) {
$max_sum = $current_sum;
}
}
return $max_sum;
}
// Test cases
echo maxFreeTime(5, 1, array(1, 3), array(2, 5)) . "\n"; // Output: 2
echo maxFreeTime(10, 1, array(0, 2, 9), array(1, 4, 10)) . "\n"; // Output: 6
echo maxFreeTime(5, 2, array(0, 1, 2, 3, 4), array(1, 2, 3, 4, 5)) . "\n"; // Output: 0
?> Explanation:
This approach efficiently computes the solution by leveraging the properties of non-negative gaps and the sliding window technique to find the optimal segment of gaps to merge, ensuring optimal performance even for large inputs. |
Beta Was this translation helpful? Give feedback.
We need to maximize the longest continuous period of free time during an event by rescheduling at most
k
meetings. The meetings are non-overlapping and must maintain their relative order after rescheduling. The key insight is that rescheduling up tok
meetings allows us to merge up tok+1
consecutive gaps between meetings into a single contiguous free block. The solution involves calculating these gaps and using a sliding window technique to find the maximum sum of anyk+1
consecutive gaps.Approach