-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeeting.cs
More file actions
106 lines (90 loc) · 3.42 KB
/
Meeting.cs
File metadata and controls
106 lines (90 loc) · 3.42 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.InterviewCake
{
[TestClass]
public class Meeting
{
public Meeting() { }
public int StartTime { get; set; }
public int EndTime { get; set; }
public Meeting(int startTime, int endTime)
{
// Number of 30 min blocks past 9:00 am
StartTime = startTime;
EndTime = endTime;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
if (ReferenceEquals(obj, this))
{
return true;
}
var meeting = (Meeting)obj;
return StartTime == meeting.StartTime && EndTime == meeting.EndTime;
}
public override int GetHashCode()
{
var result = 17;
result = result * 31 + StartTime;
result = result * 31 + EndTime;
return result;
}
public override string ToString()
{
return $"({StartTime}, {EndTime})";
}
public static List<Meeting> MergeRanges(List<Meeting> meetings)
{
// Meeting(1,3) overlaps with Meeting(2,4) Merge to Meeting(1,4)
// SO, if the end time is greater than the start time of next then merge the two meetings.
// Sort the original meeting requests by start time.
var sortedMeetingList = meetings
.Select(m => new Meeting(m.StartTime, m.EndTime))
.OrderBy(m => m.StartTime);
// Initialize the new list to the first item in the sorted list.
IList<Meeting> ouputMeetingList = new List<Meeting> { sortedMeetingList.First() };
foreach (var meeting in sortedMeetingList)
{
// get the lastly added item to the output list.
var lastAddedMeeting = ouputMeetingList.Last();
// If the start time of the incoming list is less than the end time of the existing list.
// As it is ordered by start time, we can merge to the existing list.
if (meeting.StartTime <= lastAddedMeeting.EndTime)
{
// Update the end time to the max end time of both the meeting request.
// For instance Meeting(1,3), the new meeting could be Meeting(1,2) => result should be Meeting (1,3)
// Second case: Meeting(1,3), the new meeting coule be Meeting(2, 4) => result should be Meeting (1,4)
lastAddedMeeting.EndTime = Math.Max(meeting.EndTime, lastAddedMeeting.EndTime);
}
else
{
ouputMeetingList.Add(meeting);
}
}
return ouputMeetingList.ToList();
}
[TestMethod]
public void MeetingsOverlapTest()
{
var meetings = new List<Meeting>()
{
new Meeting(1, 3), new Meeting(2, 4)
};
var actual = MergeRanges(meetings);
var expected = new List<Meeting>()
{
new Meeting(1, 4)
};
Assert.AreEqual(expected.Count, actual.Count);
}
}
}