Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f72dc4f

Browse files
committed
Sync LeetCode submission Runtime - 173 ms (28.59%), Memory - 53.1 MB (94.66%)
1 parent b8e4a00 commit f72dc4f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>You are given an array of <code>events</code> where <code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>]</code>. Every event <code>i</code> starts at <code>startDay<sub>i</sub></code><sub> </sub>and ends at <code>endDay<sub>i</sub></code>.</p>
2+
3+
<p>You can attend an event <code>i</code> at any day <code>d</code> where <code>startTime<sub>i</sub> &lt;= d &lt;= endTime<sub>i</sub></code>. You can only attend one event at any time <code>d</code>.</p>
4+
5+
<p>Return <em>the maximum number of events you can attend</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/05/e1.png" style="width: 400px; height: 267px;" />
10+
<pre>
11+
<strong>Input:</strong> events = [[1,2],[2,3],[3,4]]
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong> You can attend all the three events.
14+
One way to attend them all is as shown.
15+
Attend the first event on day 1.
16+
Attend the second event on day 2.
17+
Attend the third event on day 3.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> events= [[1,2],[2,3],[3,4],[1,2]]
24+
<strong>Output:</strong> 4
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>1 &lt;= events.length &lt;= 10<sup>5</sup></code></li>
32+
<li><code>events[i].length == 2</code></li>
33+
<li><code>1 &lt;= startDay<sub>i</sub> &lt;= endDay<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
34+
</ul>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Approach: Greedy
2+
3+
# n = len(events), T = max(event[1] for event in events)
4+
# Time: O((T + n) log n)
5+
# Space: O(n)
6+
7+
import heapq
8+
9+
class Solution:
10+
def maxEvents(self, events: List[List[int]]) -> int:
11+
n = len(events)
12+
max_day = max(event[1] for event in events)
13+
events.sort()
14+
pq = []
15+
ans, j = 0, 0
16+
17+
for i in range(1, max_day + 1):
18+
while j < n and events[j][0] <= i:
19+
heapq.heappush(pq, events[j][1])
20+
j += 1
21+
while pq and pq[0] < i:
22+
heapq.heappop(pq)
23+
if pq:
24+
heapq.heappop(pq)
25+
ans += 1
26+
27+
return ans
28+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /