Skip to content

Commit 7270897

Browse files
committed
Sync LeetCode submission Runtime - 242 ms (68.21%), Memory - 38.4 MB (95.38%)
1 parent ece0ac4 commit 7270897

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<p>You are given an integer <code>eventTime</code> denoting the duration of an event. You are also given two integer arrays <code>startTime</code> and <code>endTime</code>, each of length <code>n</code>.</p>
2+
3+
<p>These represent the start and end times of <code>n</code> <strong>non-overlapping</strong> meetings that occur during the event between time <code>t = 0</code> and time <code>t = eventTime</code>, where the <code>i<sup>th</sup></code> meeting occurs during the time <code>[startTime[i], endTime[i]].</code></p>
4+
5+
<p>You can reschedule <strong>at most </strong>one meeting by moving its start time while maintaining the <strong>same duration</strong>, such that the meetings remain non-overlapping, to <strong>maximize</strong> the <strong>longest</strong> <em>continuous period of free time</em> during the event.</p>
6+
7+
<p>Return the <strong>maximum</strong> amount of free time possible after rearranging the meetings.</p>
8+
9+
<p><strong>Note</strong> that the meetings can <strong>not</strong> be rescheduled to a time outside the event and they should remain non-overlapping.</p>
10+
11+
<p><strong>Note:</strong> <em>In this version</em>, it is <strong>valid</strong> for the relative ordering of the meetings to change after rescheduling one meeting.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">eventTime = 5, startTime = [1,3], endTime = [2,5]</span></p>
18+
19+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
20+
21+
<p><strong>Explanation:</strong></p>
22+
23+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/22/example0_rescheduled.png" style="width: 375px; height: 123px;" /></p>
24+
25+
<p>Reschedule the meeting at <code>[1, 2]</code> to <code>[2, 3]</code>, leaving no meetings during the time <code>[0, 2]</code>.</p>
26+
</div>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">eventTime = 10, startTime = [0,7,9], endTime = [1,8,10]</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">7</span></p>
34+
35+
<p><strong>Explanation:</strong></p>
36+
37+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/22/rescheduled_example0.png" style="width: 375px; height: 125px;" /></p>
38+
39+
<p>Reschedule the meeting at <code>[0, 1]</code> to <code>[8, 9]</code>, leaving no meetings during the time <code>[0, 7]</code>.</p>
40+
</div>
41+
42+
<p><strong class="example">Example 3:</strong></p>
43+
44+
<div class="example-block">
45+
<p><strong>Input:</strong> <span class="example-io">eventTime = 10, startTime = [0,3,7,9], endTime = [1,4,8,10]</span></p>
46+
47+
<p><strong>Output:</strong> 6</p>
48+
49+
<p><strong>Explanation:</strong></p>
50+
51+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/image3.png" style="width: 375px; height: 125px;" /></strong></p>
52+
53+
<p>Reschedule the meeting at <code>[3, 4]</code> to <code>[8, 9]</code>, leaving no meetings during the time <code>[1, 7]</code>.</p>
54+
</div>
55+
56+
<p><strong class="example">Example 4:</strong></p>
57+
58+
<div class="example-block">
59+
<p><strong>Input:</strong> <span class="example-io">eventTime = 5, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]</span></p>
60+
61+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
62+
63+
<p><strong>Explanation:</strong></p>
64+
65+
<p>There is no time during the event not occupied by meetings.</p>
66+
</div>
67+
68+
<p>&nbsp;</p>
69+
<p><strong>Constraints:</strong></p>
70+
71+
<ul>
72+
<li><code>1 &lt;= eventTime &lt;= 10<sup>9</sup></code></li>
73+
<li><code>n == startTime.length == endTime.length</code></li>
74+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
75+
<li><code>0 &lt;= startTime[i] &lt; endTime[i] &lt;= eventTime</code></li>
76+
<li><code>endTime[i] &lt;= startTime[i + 1]</code> where <code>i</code> lies in the range <code>[0, n - 2]</code>.</li>
77+
</ul>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def maxFreeTime(
3+
self, eventTime: int, startTime: list[int], endTime: list[int]
4+
) -> int:
5+
n = len(startTime)
6+
q = [False] * n
7+
t1 = 0
8+
t2 = 0
9+
for i in range(n):
10+
if endTime[i] - startTime[i] <= t1:
11+
q[i] = True
12+
t1 = max(t1, startTime[i] - (0 if i == 0 else endTime[i - 1]))
13+
14+
if endTime[n - i - 1] - startTime[n - i - 1] <= t2:
15+
q[n - i - 1] = True
16+
t2 = max(
17+
t2,
18+
(eventTime if i == 0 else startTime[n - i])
19+
- endTime[n - i - 1],
20+
)
21+
22+
res = 0
23+
for i in range(n):
24+
left = 0 if i == 0 else endTime[i - 1]
25+
right = eventTime if i == n - 1 else startTime[i + 1]
26+
if q[i]:
27+
res = max(res, right - left)
28+
else:
29+
res = max(res, right - left - (endTime[i] - startTime[i]))
30+
return res

0 commit comments

Comments
 (0)