Skip to content

1229. Meeting Scheduler 👍

  • Time: $O(\texttt{sort}(\texttt{slots1}) + \texttt{sort}(\texttt{slots2}))$
  • Space: $O(\texttt{sort}(\texttt{slots1}) + \texttt{sort}(\texttt{slots2}))$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
 public:
  vector<int> minAvailableDuration(vector<vector<int>>& slots1,
                                   vector<vector<int>>& slots2, int duration) {
    ranges::sort(slots1);
    ranges::sort(slots2);

    int i = 0;  // slots1's index
    int j = 0;  // slots2's index

    while (i < slots1.size() && j < slots2.size()) {
      const int start = max(slots1[i][0], slots2[j][0]);
      const int end = min(slots1[i][1], slots2[j][1]);
      if (start + duration <= end)
        return {start, start + duration};
      if (slots1[i][1] < slots2[j][1])
        ++i;
      else
        ++j;
    }

    return {};
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
  public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
    Arrays.sort(slots1, (a, b) -> Integer.compare(a[0], b[0]));
    Arrays.sort(slots2, (a, b) -> Integer.compare(a[0], b[0]));

    int i = 0; // slots1's index
    int j = 0; // slots2's index

    while (i < slots1.length && j < slots2.length) {
      final int start = Math.max(slots1[i][0], slots2[j][0]);
      final int end = Math.min(slots1[i][1], slots2[j][1]);
      if (start + duration <= end)
        return Arrays.asList(start, start + duration);
      if (slots1[i][1] < slots2[j][1])
        ++i;
      else
        ++j;
    }

    return new ArrayList<>();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
  def minAvailableDuration(
      self,
      slots1: list[list[int]],
      slots2: list[list[int]],
      duration: int,
  ) -> list[int]:
    slots1.sort()
    slots2.sort()

    i = 0  # slots1's index
    j = 0  # slots2's index

    while i < len(slots1) and j < len(slots2):
      start = max(slots1[i][0], slots2[j][0])
      end = min(slots1[i][1], slots2[j][1])
      if start + duration <= end:
        return [start, start + duration]
      if slots1[i][1] < slots2[j][1]:
        i += 1
      else:
        j += 1

    return []