Skip to content

Commit fe5cddc

Browse files
authored
feat: add solutions to lc problem: No.2054 (#4076)
No.2054.Two Best Non-Overlapping Events
1 parent 68eed49 commit fe5cddc

File tree

8 files changed

+160
-97
lines changed

8 files changed

+160
-97
lines changed

solution/2000-2099/2054.Two Best Non-Overlapping Events/README.md

+44-7
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ tags:
7575

7676
### 方法一:排序 + 二分查找
7777

78-
时间复杂度 $O(n \times \log n)$,其中 $n$ 表示 $events$ 的长度。
78+
我们可以讲活动按照开始排序,然后预处理出以每个活动为作为开始的最大价值,即 $f[i]$ 表示从第 $i$ 个活动开始,到最后一个活动结束,选择其中一个活动的最大价值。
79+
80+
然后我们枚举每个活动,对于每个活动,我们使用二分查找找到第一个开始时间大于当前活动结束时间的活动,下标记为 $\textit{idx}$,那么以当前活动为开始的最大价值就是 $f[\textit{idx}]$,加上当前活动的价值,即为以当前活动为第一个活动,最终能获得的最大价值。求最大值即可。
81+
82+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为活动的数量。
7983

8084
<!-- tabs:start -->
8185

@@ -137,22 +141,27 @@ class Solution {
137141
class Solution {
138142
public:
139143
int maxTwoEvents(vector<vector<int>>& events) {
140-
sort(events.begin(), events.end());
144+
ranges::sort(events);
141145
int n = events.size();
142146
vector<int> f(n + 1);
143-
for (int i = n - 1; ~i; --i) f[i] = max(f[i + 1], events[i][2]);
147+
for (int i = n - 1; ~i; --i) {
148+
f[i] = max(f[i + 1], events[i][2]);
149+
}
144150
int ans = 0;
145-
for (auto& e : events) {
151+
for (const auto& e : events) {
146152
int v = e[2];
147153
int left = 0, right = n;
148154
while (left < right) {
149155
int mid = (left + right) >> 1;
150-
if (events[mid][0] > e[1])
156+
if (events[mid][0] > e[1]) {
151157
right = mid;
152-
else
158+
} else {
153159
left = mid + 1;
160+
}
161+
}
162+
if (left < n) {
163+
v += f[left];
154164
}
155-
if (left < n) v += f[left];
156165
ans = max(ans, v);
157166
}
158167
return ans;
@@ -193,6 +202,34 @@ func maxTwoEvents(events [][]int) int {
193202
}
194203
```
195204

205+
#### TypeScript
206+
207+
```ts
208+
function maxTwoEvents(events: number[][]): number {
209+
events.sort((a, b) => a[0] - b[0]);
210+
const n = events.length;
211+
const f: number[] = Array(n + 1).fill(0);
212+
for (let i = n - 1; ~i; --i) {
213+
f[i] = Math.max(f[i + 1], events[i][2]);
214+
}
215+
let ans = 0;
216+
for (const [_, end, v] of events) {
217+
let [left, right] = [0, n];
218+
while (left < right) {
219+
const mid = (left + right) >> 1;
220+
if (events[mid][0] > end) {
221+
right = mid;
222+
} else {
223+
left = mid + 1;
224+
}
225+
}
226+
const t = left < n ? f[left] : 0;
227+
ans = Math.max(ans, t + v);
228+
}
229+
return ans;
230+
}
231+
```
232+
196233
<!-- tabs:end -->
197234

198235
<!-- solution:end -->

solution/2000-2099/2054.Two Best Non-Overlapping Events/README_EN.md

+46-7
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Sorting + Binary Search
72+
73+
We can sort the events by their start times, and then preprocess the maximum value starting from each event, i.e., $f[i]$ represents the maximum value of choosing one event from the $i$-th event to the last event.
74+
75+
Then we enumerate each event. For each event, we use binary search to find the first event whose start time is greater than the end time of the current event, denoted as $\textit{idx}$. The maximum value starting from the current event is $f[\textit{idx}]$ plus the value of the current event, which is the maximum value that can be obtained by choosing the current event as the first event. We take the maximum value among all these values.
76+
77+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of events.
7278

7379
<!-- tabs:start -->
7480

@@ -130,22 +136,27 @@ class Solution {
130136
class Solution {
131137
public:
132138
int maxTwoEvents(vector<vector<int>>& events) {
133-
sort(events.begin(), events.end());
139+
ranges::sort(events);
134140
int n = events.size();
135141
vector<int> f(n + 1);
136-
for (int i = n - 1; ~i; --i) f[i] = max(f[i + 1], events[i][2]);
142+
for (int i = n - 1; ~i; --i) {
143+
f[i] = max(f[i + 1], events[i][2]);
144+
}
137145
int ans = 0;
138-
for (auto& e : events) {
146+
for (const auto& e : events) {
139147
int v = e[2];
140148
int left = 0, right = n;
141149
while (left < right) {
142150
int mid = (left + right) >> 1;
143-
if (events[mid][0] > e[1])
151+
if (events[mid][0] > e[1]) {
144152
right = mid;
145-
else
153+
} else {
146154
left = mid + 1;
155+
}
156+
}
157+
if (left < n) {
158+
v += f[left];
147159
}
148-
if (left < n) v += f[left];
149160
ans = max(ans, v);
150161
}
151162
return ans;
@@ -186,6 +197,34 @@ func maxTwoEvents(events [][]int) int {
186197
}
187198
```
188199

200+
#### TypeScript
201+
202+
```ts
203+
function maxTwoEvents(events: number[][]): number {
204+
events.sort((a, b) => a[0] - b[0]);
205+
const n = events.length;
206+
const f: number[] = Array(n + 1).fill(0);
207+
for (let i = n - 1; ~i; --i) {
208+
f[i] = Math.max(f[i + 1], events[i][2]);
209+
}
210+
let ans = 0;
211+
for (const [_, end, v] of events) {
212+
let [left, right] = [0, n];
213+
while (left < right) {
214+
const mid = (left + right) >> 1;
215+
if (events[mid][0] > end) {
216+
right = mid;
217+
} else {
218+
left = mid + 1;
219+
}
220+
}
221+
const t = left < n ? f[left] : 0;
222+
ans = Math.max(ans, t + v);
223+
}
224+
return ans;
225+
}
226+
```
227+
189228
<!-- tabs:end -->
190229

191230
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
class Solution {
22
public:
33
int maxTwoEvents(vector<vector<int>>& events) {
4-
sort(events.begin(), events.end());
4+
ranges::sort(events);
55
int n = events.size();
66
vector<int> f(n + 1);
7-
for (int i = n - 1; ~i; --i) f[i] = max(f[i + 1], events[i][2]);
7+
for (int i = n - 1; ~i; --i) {
8+
f[i] = max(f[i + 1], events[i][2]);
9+
}
810
int ans = 0;
9-
for (auto& e : events) {
11+
for (const auto& e : events) {
1012
int v = e[2];
1113
int left = 0, right = n;
1214
while (left < right) {
1315
int mid = (left + right) >> 1;
14-
if (events[mid][0] > e[1])
16+
if (events[mid][0] > e[1]) {
1517
right = mid;
16-
else
18+
} else {
1719
left = mid + 1;
20+
}
21+
}
22+
if (left < n) {
23+
v += f[left];
1824
}
19-
if (left < n) v += f[left];
2025
ans = max(ans, v);
2126
}
2227
return ans;
2328
}
24-
};
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function maxTwoEvents(events: number[][]): number {
2+
events.sort((a, b) => a[0] - b[0]);
3+
const n = events.length;
4+
const f: number[] = Array(n + 1).fill(0);
5+
for (let i = n - 1; ~i; --i) {
6+
f[i] = Math.max(f[i + 1], events[i][2]);
7+
}
8+
let ans = 0;
9+
for (const [_, end, v] of events) {
10+
let [left, right] = [0, n];
11+
while (left < right) {
12+
const mid = (left + right) >> 1;
13+
if (events[mid][0] > end) {
14+
right = mid;
15+
} else {
16+
left = mid + 1;
17+
}
18+
}
19+
const t = left < n ? f[left] : 0;
20+
ans = Math.max(ans, t + v);
21+
}
22+
return ans;
23+
}

solution/2000-2099/2062.Count Vowel Substrings of a String/README.md

+10-30
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ tags:
9595
```python
9696
class Solution:
9797
def countVowelSubstrings(self, word: str) -> int:
98-
n = len(word)
99-
s = set('aeiou')
100-
return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
98+
s = set("aeiou")
99+
ans, n = 0, len(word)
100+
for i in range(n):
101+
t = set()
102+
for c in word[i:]:
103+
if c not in s:
104+
break
105+
t.add(c)
106+
ans += len(t) == 5
107+
return ans
101108
```
102109

103110
#### Java
@@ -204,31 +211,4 @@ function countVowelSubstrings(word: string): number {
204211

205212
<!-- solution:end -->
206213

207-
<!-- solution:start -->
208-
209-
### 方法二
210-
211-
<!-- tabs:start -->
212-
213-
#### Python3
214-
215-
```python
216-
class Solution:
217-
def countVowelSubstrings(self, word: str) -> int:
218-
s = set('aeiou')
219-
ans, n = 0, len(word)
220-
for i in range(n):
221-
t = set()
222-
for c in word[i:]:
223-
if c not in s:
224-
break
225-
t.add(c)
226-
ans += len(t) == 5
227-
return ans
228-
```
229-
230-
<!-- tabs:end -->
231-
232-
<!-- solution:end -->
233-
234214
<!-- problem:end -->

solution/2000-2099/2062.Count Vowel Substrings of a String/README_EN.md

+15-31
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ tags:
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Brute Force Enumeration + Hash Table
77+
78+
We can enumerate the left endpoint $i$ of the substring. For the current left endpoint, maintain a hash table to record the vowels that appear in the current substring. Then enumerate the right endpoint $j$. If the character at the current right endpoint is not a vowel, break the loop. Otherwise, add the character at the current right endpoint to the hash table. If the number of elements in the hash table is $5$, it means the current substring is a vowel substring, and increment the result by $1$.
79+
80+
The time complexity is $O(n^2)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string $word$, and $C$ is the size of the character set, which is $5$ in this problem.
7781

7882
<!-- tabs:start -->
7983

@@ -82,9 +86,16 @@ tags:
8286
```python
8387
class Solution:
8488
def countVowelSubstrings(self, word: str) -> int:
85-
n = len(word)
86-
s = set('aeiou')
87-
return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
89+
s = set("aeiou")
90+
ans, n = 0, len(word)
91+
for i in range(n):
92+
t = set()
93+
for c in word[i:]:
94+
if c not in s:
95+
break
96+
t.add(c)
97+
ans += len(t) == 5
98+
return ans
8899
```
89100

90101
#### Java
@@ -191,31 +202,4 @@ function countVowelSubstrings(word: string): number {
191202

192203
<!-- solution:end -->
193204

194-
<!-- solution:start -->
195-
196-
### Solution 2
197-
198-
<!-- tabs:start -->
199-
200-
#### Python3
201-
202-
```python
203-
class Solution:
204-
def countVowelSubstrings(self, word: str) -> int:
205-
s = set('aeiou')
206-
ans, n = 0, len(word)
207-
for i in range(n):
208-
t = set()
209-
for c in word[i:]:
210-
if c not in s:
211-
break
212-
t.add(c)
213-
ans += len(t) == 5
214-
return ans
215-
```
216-
217-
<!-- tabs:end -->
218-
219-
<!-- solution:end -->
220-
221205
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
class Solution:
22
def countVowelSubstrings(self, word: str) -> int:
3-
n = len(word)
4-
s = set('aeiou')
5-
return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
3+
s = set("aeiou")
4+
ans, n = 0, len(word)
5+
for i in range(n):
6+
t = set()
7+
for c in word[i:]:
8+
if c not in s:
9+
break
10+
t.add(c)
11+
ans += len(t) == 5
12+
return ans

solution/2000-2099/2062.Count Vowel Substrings of a String/Solution2.py

-12
This file was deleted.

0 commit comments

Comments
 (0)