Skip to content

Commit 50a77d8

Browse files
add 1353
1 parent c27c731 commit 50a77d8

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1353|[Maximum Number of Events That Can Be Attended](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1353.java) | |Medium|Greedy, Sort, Segment Tree|
1112
|1352|[Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1352.java) | |Medium|Array, Design|
1213
|1351|[Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1351.java) | |Easy|Array, Binary Search|
1314
|1349|[Maximum Students Taking Exam](https://leetcode.com/problems/maximum-students-taking-exam/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1349.java) | |Hard|Dynamic Programming|
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.PriorityQueue;
8+
9+
/**
10+
* 1353. Maximum Number of Events That Can Be Attended
11+
*
12+
* Given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.
13+
* You can attend an event i at any day d where startTimei <= d <= endTimei. Notice that you can only attend one event at any time d.
14+
* Return the maximum number of events you can attend.
15+
*
16+
* Example 1:
17+
* Input: events = [[1,2],[2,3],[3,4]]
18+
* Output: 3
19+
* Explanation: You can attend all the three events.
20+
* One way to attend them all is as shown.
21+
* Attend the first event on day 1.
22+
* Attend the second event on day 2.
23+
* Attend the third event on day 3.
24+
*
25+
* Example 2:
26+
* Input: events= [[1,2],[2,3],[3,4],[1,2]]
27+
* Output: 4
28+
*
29+
* Example 3:
30+
* Input: events = [[1,4],[4,4],[2,2],[3,4],[1,1]]
31+
* Output: 4
32+
*
33+
* Example 4:
34+
* Input: events = [[1,100000]]
35+
* Output: 1
36+
*
37+
* Example 5:
38+
* Input: events = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7]]
39+
* Output: 7
40+
*
41+
* Constraints:
42+
* 1 <= events.length <= 10^5
43+
* events[i].length == 2
44+
* 1 <= events[i][0] <= events[i][1] <= 10^5
45+
* */
46+
public class _1353 {
47+
public static class Solution1 {
48+
/**
49+
* Credit: https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/discuss/510263/JavaC%2B%2BPython-Priority-Queue
50+
*
51+
* 1. Sort events by start time, if ties, by end time;
52+
* 2. From day 1 to day 100,000, we add all events that start on this day into a priorityqueue,
53+
* also, we remove the events that closed on this day from the priorityqueue;
54+
* 3. attend the event that ends on this day (earliest, i.e. greedy) and pop it out of the priorityqueue
55+
*
56+
* */
57+
public int maxEvents(int[][] events) {
58+
Arrays.sort(events, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
59+
PriorityQueue<Integer> heap = new PriorityQueue<>();
60+
int maxEvents = 0;
61+
int i = 0;
62+
for (int day = 1; day <= 100000; day++) {
63+
while (i < events.length && events[i][0] == day) {
64+
heap.offer(events[i++][1]);
65+
}
66+
while (heap.size() > 0 && heap.peek() < day) {
67+
heap.poll();
68+
}
69+
if (heap.size() > 0) {
70+
heap.poll();
71+
maxEvents++;
72+
}
73+
}
74+
return maxEvents;
75+
}
76+
}
77+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1353;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1353Test {
10+
private static _1353.Solution1 solution1;
11+
private static int[][] events;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1353.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
events = new int[][]{
21+
{1, 2},
22+
{2, 3},
23+
{3, 4}
24+
};
25+
assertEquals(3, solution1.maxEvents(events));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
events = new int[][]{
31+
{1, 2},
32+
{2, 3},
33+
{3, 4},
34+
{1, 2}
35+
};
36+
assertEquals(4, solution1.maxEvents(events));
37+
}
38+
39+
@Test
40+
public void test3() {
41+
events = new int[][]{
42+
{1, 4},
43+
{4, 4},
44+
{2, 2},
45+
{3, 4},
46+
{1, 1}
47+
};
48+
assertEquals(4, solution1.maxEvents(events));
49+
}
50+
51+
@Test
52+
public void test4() {
53+
events = new int[][]{
54+
{1, 100000}
55+
};
56+
assertEquals(1, solution1.maxEvents(events));
57+
}
58+
59+
@Test
60+
public void test5() {
61+
events = new int[][]{
62+
{1, 1},
63+
{1, 2},
64+
{1, 3},
65+
{1, 4},
66+
{1, 5},
67+
{1, 6},
68+
{1, 7},
69+
};
70+
assertEquals(7, solution1.maxEvents(events));
71+
}
72+
73+
}

0 commit comments

Comments
 (0)