Skip to content

Commit 7abeba3

Browse files
committed
Sync LeetCode submission Runtime - 219 ms (26.70%), Memory - 52.9 MB (30.73%)
1 parent 5412d19 commit 7abeba3

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of meeting <code>i</code> (inclusive).</p>
2+
3+
<p>Return the count of days when the employee is available for work but no meetings are scheduled.</p>
4+
5+
<p><strong>Note: </strong>The meetings may overlap.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<div class="example-block">
11+
<p><strong>Input:</strong> <span class="example-io">days = 10, meetings = [[5,7],[1,3],[9,10]]</span></p>
12+
13+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
14+
15+
<p><strong>Explanation:</strong></p>
16+
17+
<p>There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.</p>
18+
</div>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<div class="example-block">
23+
<p><strong>Input:</strong> <span class="example-io">days = 5, meetings = [[2,4],[1,3]]</span></p>
24+
25+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
26+
27+
<p><strong>Explanation:</strong></p>
28+
29+
<p>There is no meeting scheduled on the 5<sup>th </sup>day.</p>
30+
</div>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">days = 6, meetings = [[1,6]]</span></p>
36+
37+
<p><strong>Output:</strong> 0</p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<p>Meetings are scheduled for all working days.</p>
42+
</div>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= days &lt;= 10<sup>9</sup></code></li>
49+
<li><code>1 &lt;= meetings.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>meetings[i].length == 2</code></li>
51+
<li><code><font face="monospace">1 &lt;= meetings[i][0] &lt;= meetings[i][1] &lt;= days</font></code></li>
52+
</ul>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Approach 2: Sorting
2+
3+
# Time: O(n log n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def countDays(self, days: int, meetings: List[List[int]]) -> int:
8+
free_days = 0
9+
latest_end = 0
10+
11+
meetings.sort()
12+
13+
for start, end in meetings:
14+
if start > latest_end + 1:
15+
free_days += start - latest_end - 1
16+
17+
latest_end = max(latest_end, end)
18+
19+
free_days += days - latest_end
20+
21+
return free_days
22+

0 commit comments

Comments
 (0)