LeetCode 题解工作台

重新安排会议得到最多空余时间 II

给你一个整数 eventTime 表示一个活动的总时长,这个活动开始于 t = 0 ,结束于 t = eventTime 。 同时给你两个长度为 n 的整数数组 startTime 和 endTime 。它们表示这次活动中 n 个时间 没有重叠 的会议,其中第 i 个会议的时间为 [startTim…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 贪心·invariant

bolt

答案摘要

根据题目描述,对于会议 ,我们记它左侧非空闲位置为 ,右侧非空闲位置为 ,记会议 的时长为 $w_i = \text{endTime}[i] - \text{startTime}[i]$,则: $$

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数 eventTime 表示一个活动的总时长,这个活动开始于 t = 0 ,结束于 t = eventTime 。

同时给你两个长度为 n 的整数数组 startTime 和 endTime 。它们表示这次活动中 n 个时间 没有重叠 的会议,其中第 i 个会议的时间为 [startTime[i], endTime[i]] 。

你可以重新安排 至多 一个会议,安排的规则是将会议时间平移,且保持原来的 会议时长 ,你的目的是移动会议后 最大化 最长 连续空余时间。

请你返回重新安排会议以后,可以得到的 最大 空余时间。

注意,会议 不能 安排到整个活动的时间以外,且会议之间需要保持互不重叠。

注意:重新安排会议以后,会议之间的顺序可以发生改变。

 

示例 1:

输入:eventTime = 5, startTime = [1,3], endTime = [2,5]

输出:2

解释:

将 [1, 2] 的会议安排到 [2, 3] ,得到空余时间 [0, 2] 。

示例 2:

输入:eventTime = 10, startTime = [0,7,9], endTime = [1,8,10]

输出:7

解释:

将 [0, 1] 的会议安排到 [8, 9] ,得到空余时间 [0, 7] 。

示例 3:

输入:eventTime = 10, startTime = [0,3,7,9], endTime = [1,4,8,10]

输出:6

解释:

将 [3, 4] 的会议安排到 [8, 9] ,得到空余时间 [1, 7] 。

示例 4:

输入:eventTime = 5, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]

输出:0

解释:

活动中的所有时间都被会议安排满了。

 

提示:

  • 1 <= eventTime <= 109
  • n == startTime.length == endTime.length
  • 2 <= n <= 105
  • 0 <= startTime[i] < endTime[i] <= eventTime
  • endTime[i] <= startTime[i + 1] 其中 i 在范围 [0, n - 2] 之间。
lightbulb

解题思路

方法一:贪心

根据题目描述,对于会议 ii,我们记它左侧非空闲位置为 lil_i,右侧非空闲位置为 rir_i,记会议 ii 的时长为 wi=endTime[i]startTime[i]w_i = \text{endTime}[i] - \text{startTime}[i],则:

li={0i=0endTime[i1]i>0l_i = \begin{cases} 0 & i = 0 \\\\ \text{endTime}[i - 1] & i \gt 0 \end{cases} ri={eventTimei=n1startTime[i+1]i<n1r_i = \begin{cases} \text{eventTime} & i = n - 1 \\\\ \text{startTime}[i + 1] & i \lt n - 1 \end{cases}

那么它可以向左移动,也可以向右移动,此时空闲时间为:

riliwir_i - l_i - w_i

如果左侧存在最大的空闲时间 prei1\text{pre}_{i - 1},满足 prei1wi\text{pre}_{i - 1} \geq w_i,则可以将会议 ii 向左移动到该位置,得到新的空闲时间:

rilir_i - l_i

同理,如果右侧存在最大的空闲时间 sufi+1\text{suf}_{i + 1},满足 sufi+1wi\text{suf}_{i + 1} \geq w_i,则可以将会议 ii 向右移动到该位置,得到新的空闲时间:

rilir_i - l_i

因此,我们首先预处理两个数组 pre\text{pre}suf\text{suf},其中 pre[i]\text{pre}[i] 表示 [0,i][0, i] 范围内的最大空闲时间,suf[i]\text{suf}[i] 表示 [i,n1][i, n - 1] 范围内的最大空闲时间。然后遍历每个会议 ii,计算它移动后的最大空闲时间,取最大值即可。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 为会议的数量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def maxFreeTime(
        self, eventTime: int, startTime: List[int], endTime: List[int]
    ) -> int:
        n = len(startTime)
        pre = [0] * n
        suf = [0] * n
        pre[0] = startTime[0]
        suf[n - 1] = eventTime - endTime[-1]
        for i in range(1, n):
            pre[i] = max(pre[i - 1], startTime[i] - endTime[i - 1])
        for i in range(n - 2, -1, -1):
            suf[i] = max(suf[i + 1], startTime[i + 1] - endTime[i])
        ans = 0
        for i in range(n):
            l = 0 if i == 0 else endTime[i - 1]
            r = eventTime if i == n - 1 else startTime[i + 1]
            w = endTime[i] - startTime[i]
            ans = max(ans, r - l - w)
            if i and pre[i - 1] >= w:
                ans = max(ans, r - l)
            elif i + 1 < n and suf[i + 1] >= w:
                ans = max(ans, r - l)
        return ans
speed

复杂度分析

指标
时间complexity is O(n) as each meeting is evaluated once for potential rescheduling. Space complexity is O(1) if only tracking current maximum gaps without additional arrays.
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Checking if the candidate meeting can extend the largest gap indicates awareness of greedy optimization.

  • question_mark

    Confirming non-overlapping constraints after rescheduling shows understanding of invariant maintenance.

  • question_mark

    Evaluating all possible single reschedules signals thorough exploration of array-based solutions.

warning

常见陷阱

外企场景
  • error

    Forgetting to maintain the duration of the rescheduled meeting, which can invalidate gaps.

  • error

    Overlooking edge gaps at the start or end of the event when computing maximum free time.

  • error

    Assuming multiple meetings can be moved instead of only one, violating problem constraints.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow rescheduling multiple meetings to maximize free time using a dynamic greedy approach.

  • arrow_right_alt

    Determine minimum total idle time instead of maximum free interval with a similar rescheduling strategy.

  • arrow_right_alt

    Consider meetings with flexible durations, adjusting both start and end times under non-overlap constraints.

help

常见问题

外企场景

重新安排会议得到最多空余时间 II题解:贪心·invariant | LeetCode #3440 中等