leetcode题解 meeting-scheduler

本文介绍了解决 LeetCode 上的 'meeting-scheduler' 问题的方法。通过两个时间槽数组 slots1 和 slots2,以及一个会议时长 duration,找出双方都能空出的最早时间。首先对两个时间槽数组进行排序,然后通过比较每个时间范围来找到满足条件的可用时间段。文章提供了两种解决方案,一种是分别遍历两个排序后的数组,另一种是将两个数组合并并排序后再遍历。文章还解释了为何可以合并两个时间槽,因为同一人的两个时间槽不会相交。
摘要由CSDN通过智能技术生成

题目链接:https://leetcode.com/problems/meeting-scheduler/

Given the availability time slots arrays slots1 and slots2 of two people and a meeting duration duration, return the earliest time slot that works for both of them and is of duration duration.

If there is no common time slot that satisfies the requirements, return an empty array.

The format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end.

It is guaranteed that no two availability slots of the same person intersect with each other. That is, for any two time slots [start1, end1] and [start2, end2] of the same person, either start1 > end2 or start2 > end1.

Example 1:

Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]

Example 2:

Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []

Constraints:

1 <= slots1.length, slots2.length <= 10^4
slots1[i].length, slots2[i].length == 2
slots1[i][0] < slots1[i][1]
slots2[i][0] < slots2[i][1]
0 <= slots1[i][j], slots2[i][j] <= 10^9
1 <= duration <= 10^6

Solution 1

sort slots1 and slots2
index i point to current slot1
index j point to current slot2

get available time range from slot1 and slot2
there are four available time ranges between slot1 and slot2

1.
slot1: ---------
slot2:    ---
in this case, the start time and end time of slot2 is within slot1
2.
slot1:    ---
slot2: ---------

3.
slot1: -----
slot2:    -----

4.
slot1:      ------
slot2: --------

By observing the 4 cases, we can get the following pattern
when the time range = min(e1,e2) - max(s1, s2) >= duration, there is an available time range,
return [max(s1, s2), max(s1, s2) + duration]

otherwise,
if e1 < e2, i++
if e1 >= e2, j++

for example, current e2 > current e1, if we move the slot2, then the next s2 > current e2 > current e1.
There is definitely no intersection between the next slot2 and current slot1.
Therefore, we just need move the slot with a smaller end time.

Time Complexity: O(mlogm + nlogn + m + n) = O(mlogm + nlogn), m is the length of slots1, n is the length of slots2
Space Complexity: O(1)

package QuestionsFromOtherSources.MeetingScheduler;

import java.util.Arrays;

/**
 * @date: 4/29/21 10:19 AM
 */
public class SolutionOne {
   
    public static int[] meetingScheduler(int[][] slots1, int[][] slots2, int duration){
   
        Arrays.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值