Skip to content

Commit 573e0e4

Browse files
committed
feat: add solutions to lc problem: No.1353
No.1353.Maximum Number of Events That Can Be Attended
1 parent afe6de6 commit 573e0e4

File tree

6 files changed

+400
-2
lines changed

6 files changed

+400
-2
lines changed

solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README.md

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,164 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:哈希表 + 贪心 + 优先队列**
53+
54+
定义哈希表记录每个会议的开始和结束时间,其中键为会议开始时间,值为结束时间列表。
55+
56+
枚举当前时间 $s$,找出所有开始时间等于当前时间的会议,将其结束时间加入优先队列(小根堆)中。同时,优先队列要移除所有结束时间小于当前时间的会议。
57+
58+
然后从优先队列中取出结束时间最小的会议,即为当前时间可以参加的会议,累加答案数。如果优先队列为空,则说明当前时间没有可以参加的会议。
59+
60+
时间复杂度 $O(m\log n)$。其中 $m$, $n$ 分别表示会议的最大结束时间,以及会议的数量。
61+
5262
<!-- tabs:start -->
5363

5464
### **Python3**
5565

5666
<!-- 这里可写当前语言的特殊实现逻辑 -->
5767

5868
```python
59-
69+
class Solution:
70+
def maxEvents(self, events: List[List[int]]) -> int:
71+
d = defaultdict(list)
72+
i, j = inf, 0
73+
for s, e in events:
74+
d[s].append(e)
75+
i = min(i, s)
76+
j = max(j, e)
77+
h = []
78+
ans = 0
79+
for s in range(i, j + 1):
80+
while h and h[0] < s:
81+
heappop(h)
82+
for e in d[s]:
83+
heappush(h, e)
84+
if h:
85+
ans += 1
86+
heappop(h)
87+
return ans
6088
```
6189

6290
### **Java**
6391

6492
<!-- 这里可写当前语言的特殊实现逻辑 -->
6593

6694
```java
95+
class Solution {
96+
public int maxEvents(int[][] events) {
97+
Map<Integer, List<Integer>> d = new HashMap<>();
98+
int i = Integer.MAX_VALUE, j = 0;
99+
for (var v : events) {
100+
int s = v[0], e = v[1];
101+
d.computeIfAbsent(s, k -> new ArrayList<>()).add(e);
102+
i = Math.min(i, s);
103+
j = Math.max(j, e);
104+
}
105+
PriorityQueue<Integer> q = new PriorityQueue<>();
106+
int ans = 0;
107+
for (int s = i; s <= j; ++s) {
108+
while (!q.isEmpty() && q.peek() < s) {
109+
q.poll();
110+
}
111+
for (int e : d.getOrDefault(s, Collections.emptyList())) {
112+
q.offer(e);
113+
}
114+
if (!q.isEmpty()) {
115+
q.poll();
116+
++ans;
117+
}
118+
}
119+
return ans;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int maxEvents(vector<vector<int>>& events) {
130+
unordered_map<int, vector<int>> d;
131+
int i = INT_MAX, j = 0;
132+
for (auto& v : events) {
133+
int s = v[0], e = v[1];
134+
d[s].push_back(e);
135+
i = min(i, s);
136+
j = max(j, e);
137+
}
138+
priority_queue<int, vector<int>, greater<int>> q;
139+
int ans = 0;
140+
for (int s = i; s <= j; ++s) {
141+
while (q.size() && q.top() < s) {
142+
q.pop();
143+
}
144+
for (int e : d[s]) {
145+
q.push(e);
146+
}
147+
if (q.size()) {
148+
++ans;
149+
q.pop();
150+
}
151+
}
152+
return ans;
153+
}
154+
};
155+
```
67156
157+
### **Go**
158+
159+
```go
160+
func maxEvents(events [][]int) int {
161+
d := map[int][]int{}
162+
i, j := math.MaxInt32, 0
163+
for _, v := range events {
164+
s, e := v[0], v[1]
165+
d[s] = append(d[s], e)
166+
i = min(i, s)
167+
j = max(j, e)
168+
}
169+
q := hp{}
170+
ans := 0
171+
for s := i; s <= j; s++ {
172+
for q.Len() > 0 && q.IntSlice[0] < s {
173+
heap.Pop(&q)
174+
}
175+
for _, e := range d[s] {
176+
heap.Push(&q, e)
177+
}
178+
if q.Len() > 0 {
179+
heap.Pop(&q)
180+
ans++
181+
}
182+
}
183+
return ans
184+
}
185+
186+
func max(a, b int) int {
187+
if a > b {
188+
return a
189+
}
190+
return b
191+
}
192+
193+
func min(a, b int) int {
194+
if a < b {
195+
return a
196+
}
197+
return b
198+
}
199+
200+
type hp struct{ sort.IntSlice }
201+
202+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
203+
func (h *hp) Pop() interface{} {
204+
a := h.IntSlice
205+
v := a[len(a)-1]
206+
h.IntSlice = a[:len(a)-1]
207+
return v
208+
}
209+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
68210
```
69211

70212
### **...**

solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README_EN.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,145 @@ Attend the third event on day 3.
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def maxEvents(self, events: List[List[int]]) -> int:
51+
d = defaultdict(list)
52+
i, j = inf, 0
53+
for s, e in events:
54+
d[s].append(e)
55+
i = min(i, s)
56+
j = max(j, e)
57+
h = []
58+
ans = 0
59+
for s in range(i, j + 1):
60+
while h and h[0] < s:
61+
heappop(h)
62+
for e in d[s]:
63+
heappush(h, e)
64+
if h:
65+
ans += 1
66+
heappop(h)
67+
return ans
5068
```
5169

5270
### **Java**
5371

5472
```java
73+
class Solution {
74+
public int maxEvents(int[][] events) {
75+
Map<Integer, List<Integer>> d = new HashMap<>();
76+
int i = Integer.MAX_VALUE, j = 0;
77+
for (var v : events) {
78+
int s = v[0], e = v[1];
79+
d.computeIfAbsent(s, k -> new ArrayList<>()).add(e);
80+
i = Math.min(i, s);
81+
j = Math.max(j, e);
82+
}
83+
PriorityQueue<Integer> q = new PriorityQueue<>();
84+
int ans = 0;
85+
for (int s = i; s <= j; ++s) {
86+
while (!q.isEmpty() && q.peek() < s) {
87+
q.poll();
88+
}
89+
for (int e : d.getOrDefault(s, Collections.emptyList())) {
90+
q.offer(e);
91+
}
92+
if (!q.isEmpty()) {
93+
q.poll();
94+
++ans;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int maxEvents(vector<vector<int>>& events) {
108+
unordered_map<int, vector<int>> d;
109+
int i = INT_MAX, j = 0;
110+
for (auto& v : events) {
111+
int s = v[0], e = v[1];
112+
d[s].push_back(e);
113+
i = min(i, s);
114+
j = max(j, e);
115+
}
116+
priority_queue<int, vector<int>, greater<int>> q;
117+
int ans = 0;
118+
for (int s = i; s <= j; ++s) {
119+
while (q.size() && q.top() < s) {
120+
q.pop();
121+
}
122+
for (int e : d[s]) {
123+
q.push(e);
124+
}
125+
if (q.size()) {
126+
++ans;
127+
q.pop();
128+
}
129+
}
130+
return ans;
131+
}
132+
};
133+
```
55134
135+
### **Go**
136+
137+
```go
138+
func maxEvents(events [][]int) int {
139+
d := map[int][]int{}
140+
i, j := math.MaxInt32, 0
141+
for _, v := range events {
142+
s, e := v[0], v[1]
143+
d[s] = append(d[s], e)
144+
i = min(i, s)
145+
j = max(j, e)
146+
}
147+
q := hp{}
148+
ans := 0
149+
for s := i; s <= j; s++ {
150+
for q.Len() > 0 && q.IntSlice[0] < s {
151+
heap.Pop(&q)
152+
}
153+
for _, e := range d[s] {
154+
heap.Push(&q, e)
155+
}
156+
if q.Len() > 0 {
157+
heap.Pop(&q)
158+
ans++
159+
}
160+
}
161+
return ans
162+
}
163+
164+
func max(a, b int) int {
165+
if a > b {
166+
return a
167+
}
168+
return b
169+
}
170+
171+
func min(a, b int) int {
172+
if a < b {
173+
return a
174+
}
175+
return b
176+
}
177+
178+
type hp struct{ sort.IntSlice }
179+
180+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
181+
func (h *hp) Pop() interface{} {
182+
a := h.IntSlice
183+
v := a[len(a)-1]
184+
h.IntSlice = a[:len(a)-1]
185+
return v
186+
}
187+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
56188
```
57189

58190
### **...**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int maxEvents(vector<vector<int>>& events) {
4+
unordered_map<int, vector<int>> d;
5+
int i = INT_MAX, j = 0;
6+
for (auto& v : events) {
7+
int s = v[0], e = v[1];
8+
d[s].push_back(e);
9+
i = min(i, s);
10+
j = max(j, e);
11+
}
12+
priority_queue<int, vector<int>, greater<int>> q;
13+
int ans = 0;
14+
for (int s = i; s <= j; ++s) {
15+
while (q.size() && q.top() < s) {
16+
q.pop();
17+
}
18+
for (int e : d[s]) {
19+
q.push(e);
20+
}
21+
if (q.size()) {
22+
++ans;
23+
q.pop();
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
func maxEvents(events [][]int) int {
2+
d := map[int][]int{}
3+
i, j := math.MaxInt32, 0
4+
for _, v := range events {
5+
s, e := v[0], v[1]
6+
d[s] = append(d[s], e)
7+
i = min(i, s)
8+
j = max(j, e)
9+
}
10+
q := hp{}
11+
ans := 0
12+
for s := i; s <= j; s++ {
13+
for q.Len() > 0 && q.IntSlice[0] < s {
14+
heap.Pop(&q)
15+
}
16+
for _, e := range d[s] {
17+
heap.Push(&q, e)
18+
}
19+
if q.Len() > 0 {
20+
heap.Pop(&q)
21+
ans++
22+
}
23+
}
24+
return ans
25+
}
26+
27+
func max(a, b int) int {
28+
if a > b {
29+
return a
30+
}
31+
return b
32+
}
33+
34+
func min(a, b int) int {
35+
if a < b {
36+
return a
37+
}
38+
return b
39+
}
40+
41+
type hp struct{ sort.IntSlice }
42+
43+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
44+
func (h *hp) Pop() interface{} {
45+
a := h.IntSlice
46+
v := a[len(a)-1]
47+
h.IntSlice = a[:len(a)-1]
48+
return v
49+
}
50+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }

0 commit comments

Comments
 (0)