-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
47 lines (46 loc) · 1.51 KB
/
Program.cs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
namespace MeetingScheduler
{
class Program
{
static void Main(string[] args)
{
var slots1 = new int[][]
{
new[] {10, 50},
new[] {60, 120},
new[] {140, 210},
};
var slots2 = new int[][]
{
new[] {0, 15},
new[] {60, 70},
};
int duration = 8;
Console.WriteLine(MinAvailableDuration(slots1, slots2, duration));
}
static IList<int> MinAvailableDuration(int[][] slots1, int[][] slots2, int duration)
{
slots1 = slots1.OrderBy(x => x[0]).ToArray();
slots2 = slots2.OrderBy(x => x[0]).ToArray();
int p1 = 0, p2 = 0;
while (p1 < slots1.Length && p2 < slots2.Length)
{
int start1 = slots1[p1][0], end1 = slots1[p1][1];
int start2 = slots2[p2][0], end2 = slots2[p2][1];
if (start2 <= start1 && start1 <= end2 || start1 <= start2 && start2 <= end1)
{
if (Math.Max(start1, start2) + duration <= Math.Min(end1, end2))
return new List<int> { Math.Max(start1, start2), Math.Max(start1, start2) + +duration };
}
if (end1 < end2)
p1++;
else
p2++;
}
return new List<int>();
}
}
}