-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
116 lines (102 loc) · 2.59 KB
/
Solution.cs
File metadata and controls
116 lines (102 loc) · 2.59 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
107
108
109
110
111
112
113
114
115
116
public class MyCalendarTwo
{
private readonly List<int[]> bookings;
private readonly List<int[]> overlapBookings;
public MyCalendarTwo()
{
bookings = [];
overlapBookings = [];
}
private bool doesOverlap(int start1, int end1, int start2, int end2)
{
return int.Max(start1, start2) < int.Min(end1, end2);
}
private int[] getOverlapped(int start1, int end1, int start2, int end2)
{
return [int.Max(start1, start2), int.Min(end1, end2)];
}
public bool Book(int start, int end)
{
foreach (int[] booking in overlapBookings)
{
if (doesOverlap(booking[0], booking[1], start, end))
{
return false;
}
}
foreach (int[] booking in bookings)
{
if (doesOverlap(booking[0], booking[1], start, end))
{
overlapBookings.Add(getOverlapped(booking[0], booking[1], start, end));
}
}
bookings.Add([start, end]);
return true;
}
}
public class MyCalendarTwo_2
{
private SortedDictionary<int, int> eventCount;
public MyCalendarTwo_2()
{
eventCount = [];
}
public bool Book(int start, int end)
{
if (eventCount.ContainsKey(start))
{
eventCount[start]++;
}
else
{
eventCount.Add(start, 1);
}
if (eventCount.ContainsKey(end))
{
eventCount[end]--;
}
else
{
eventCount.Add(end, -1);
}
int ongoingEvent = 0;
foreach (int count in eventCount.Values)
{
ongoingEvent += count;
if (ongoingEvent >= 3)
{
eventCount[start]--;
eventCount[end]++;
return false;
}
}
return true;
}
}
public class Solution
{
public List<string> Execute(string[] events, int[][] times)
{
MyCalendarTwo_2 myCalendar = null;
List<string> ans = [];
for (int i = 0; i < events.Length; i++)
{
if (events[i].Equals("MyCalendarTwo") || myCalendar is null)
{
myCalendar = new MyCalendarTwo_2();
ans.Add("null");
}
else
{
ans.Add(myCalendar.Book(times[i][0], times[i][1]).ToString());
}
}
return ans;
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* bool param_1 = obj.Book(start,end);
*/