Skip to content

Commit 806b640

Browse files
authored
Create 3440. Reschedule Meetings for Maximum Free Time II (#837)
2 parents c29a9b8 + 00c266a commit 806b640

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int maxFreeTime(int eventTime, vector<int>& startTime,
4+
vector<int>& endTime) {
5+
vector<int> gap(1, startTime[0]);
6+
for (int i = 1; i < startTime.size(); ++i)
7+
gap.push_back(startTime[i] - endTime[i - 1]);
8+
gap.push_back(eventTime - endTime.back());
9+
10+
vector<int> largestRight(gap.size(), 0);
11+
for (int i = gap.size() - 2; i >= 0; --i)
12+
largestRight[i] = max(largestRight[i + 1], gap[i + 1]);
13+
14+
int ans = 0, largestLeft = 0;
15+
for (int i = 1; i < gap.size(); ++i) {
16+
int curGap = endTime[i - 1] - startTime[i - 1];
17+
if (curGap <= max(largestLeft, largestRight[i]))
18+
ans = max(ans, gap[i - 1] + gap[i] + curGap);
19+
ans = max(ans, gap[i - 1] + gap[i]);
20+
largestLeft = max(largestLeft, gap[i - 1]);
21+
}
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)