feat: add solutions to lc problem: No.2054 (#4076)

No.2054.Two Best Non-Overlapping Events
This commit is contained in:
Libin YANG 2025-02-18 19:59:28 +08:00 committed by GitHub
parent 68eed4908e
commit fe5cddc0f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 160 additions and 97 deletions

View File

@ -75,7 +75,11 @@ tags:
### 方法一:排序 + 二分查找
时间复杂度 $O(n \times \log n)$,其中 $n$ 表示 $events$ 的长度。
我们可以讲活动按照开始排序,然后预处理出以每个活动为作为开始的最大价值,即 $f[i]$ 表示从第 $i$ 个活动开始,到最后一个活动结束,选择其中一个活动的最大价值。
然后我们枚举每个活动,对于每个活动,我们使用二分查找找到第一个开始时间大于当前活动结束时间的活动,下标记为 $\textit{idx}$,那么以当前活动为开始的最大价值就是 $f[\textit{idx}]$,加上当前活动的价值,即为以当前活动为第一个活动,最终能获得的最大价值。求最大值即可。
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为活动的数量。
<!-- tabs:start -->
@ -137,22 +141,27 @@ class Solution {
class Solution {
public:
int maxTwoEvents(vector<vector<int>>& events) {
sort(events.begin(), events.end());
ranges::sort(events);
int n = events.size();
vector<int> f(n + 1);
for (int i = n - 1; ~i; --i) f[i] = max(f[i + 1], events[i][2]);
for (int i = n - 1; ~i; --i) {
f[i] = max(f[i + 1], events[i][2]);
}
int ans = 0;
for (auto& e : events) {
for (const auto& e : events) {
int v = e[2];
int left = 0, right = n;
while (left < right) {
int mid = (left + right) >> 1;
if (events[mid][0] > e[1])
if (events[mid][0] > e[1]) {
right = mid;
else
} else {
left = mid + 1;
}
}
if (left < n) {
v += f[left];
}
if (left < n) v += f[left];
ans = max(ans, v);
}
return ans;
@ -193,6 +202,34 @@ func maxTwoEvents(events [][]int) int {
}
```
#### TypeScript
```ts
function maxTwoEvents(events: number[][]): number {
events.sort((a, b) => a[0] - b[0]);
const n = events.length;
const f: number[] = Array(n + 1).fill(0);
for (let i = n - 1; ~i; --i) {
f[i] = Math.max(f[i + 1], events[i][2]);
}
let ans = 0;
for (const [_, end, v] of events) {
let [left, right] = [0, n];
while (left < right) {
const mid = (left + right) >> 1;
if (events[mid][0] > end) {
right = mid;
} else {
left = mid + 1;
}
}
const t = left < n ? f[left] : 0;
ans = Math.max(ans, t + v);
}
return ans;
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -68,7 +68,13 @@ tags:
<!-- solution:start -->
### Solution 1
### Solution 1: Sorting + Binary Search
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.
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.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of events.
<!-- tabs:start -->
@ -130,22 +136,27 @@ class Solution {
class Solution {
public:
int maxTwoEvents(vector<vector<int>>& events) {
sort(events.begin(), events.end());
ranges::sort(events);
int n = events.size();
vector<int> f(n + 1);
for (int i = n - 1; ~i; --i) f[i] = max(f[i + 1], events[i][2]);
for (int i = n - 1; ~i; --i) {
f[i] = max(f[i + 1], events[i][2]);
}
int ans = 0;
for (auto& e : events) {
for (const auto& e : events) {
int v = e[2];
int left = 0, right = n;
while (left < right) {
int mid = (left + right) >> 1;
if (events[mid][0] > e[1])
if (events[mid][0] > e[1]) {
right = mid;
else
} else {
left = mid + 1;
}
}
if (left < n) {
v += f[left];
}
if (left < n) v += f[left];
ans = max(ans, v);
}
return ans;
@ -186,6 +197,34 @@ func maxTwoEvents(events [][]int) int {
}
```
#### TypeScript
```ts
function maxTwoEvents(events: number[][]): number {
events.sort((a, b) => a[0] - b[0]);
const n = events.length;
const f: number[] = Array(n + 1).fill(0);
for (let i = n - 1; ~i; --i) {
f[i] = Math.max(f[i + 1], events[i][2]);
}
let ans = 0;
for (const [_, end, v] of events) {
let [left, right] = [0, n];
while (left < right) {
const mid = (left + right) >> 1;
if (events[mid][0] > end) {
right = mid;
} else {
left = mid + 1;
}
}
const t = left < n ? f[left] : 0;
ans = Math.max(ans, t + v);
}
return ans;
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -1,24 +1,29 @@
class Solution {
public:
int maxTwoEvents(vector<vector<int>>& events) {
sort(events.begin(), events.end());
ranges::sort(events);
int n = events.size();
vector<int> f(n + 1);
for (int i = n - 1; ~i; --i) f[i] = max(f[i + 1], events[i][2]);
for (int i = n - 1; ~i; --i) {
f[i] = max(f[i + 1], events[i][2]);
}
int ans = 0;
for (auto& e : events) {
for (const auto& e : events) {
int v = e[2];
int left = 0, right = n;
while (left < right) {
int mid = (left + right) >> 1;
if (events[mid][0] > e[1])
if (events[mid][0] > e[1]) {
right = mid;
else
} else {
left = mid + 1;
}
}
if (left < n) {
v += f[left];
}
if (left < n) v += f[left];
ans = max(ans, v);
}
return ans;
}
};
};

View File

@ -0,0 +1,23 @@
function maxTwoEvents(events: number[][]): number {
events.sort((a, b) => a[0] - b[0]);
const n = events.length;
const f: number[] = Array(n + 1).fill(0);
for (let i = n - 1; ~i; --i) {
f[i] = Math.max(f[i + 1], events[i][2]);
}
let ans = 0;
for (const [_, end, v] of events) {
let [left, right] = [0, n];
while (left < right) {
const mid = (left + right) >> 1;
if (events[mid][0] > end) {
right = mid;
} else {
left = mid + 1;
}
}
const t = left < n ? f[left] : 0;
ans = Math.max(ans, t + v);
}
return ans;
}

View File

@ -95,9 +95,16 @@ tags:
```python
class Solution:
def countVowelSubstrings(self, word: str) -> int:
n = len(word)
s = set('aeiou')
return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
s = set("aeiou")
ans, n = 0, len(word)
for i in range(n):
t = set()
for c in word[i:]:
if c not in s:
break
t.add(c)
ans += len(t) == 5
return ans
```
#### Java
@ -204,31 +211,4 @@ function countVowelSubstrings(word: string): number {
<!-- solution:end -->
<!-- solution:start -->
### 方法二
<!-- tabs:start -->
#### Python3
```python
class Solution:
def countVowelSubstrings(self, word: str) -> int:
s = set('aeiou')
ans, n = 0, len(word)
for i in range(n):
t = set()
for c in word[i:]:
if c not in s:
break
t.add(c)
ans += len(t) == 5
return ans
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->

View File

@ -73,7 +73,11 @@ tags:
<!-- solution:start -->
### Solution 1
### Solution 1: Brute Force Enumeration + Hash Table
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$.
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.
<!-- tabs:start -->
@ -82,9 +86,16 @@ tags:
```python
class Solution:
def countVowelSubstrings(self, word: str) -> int:
n = len(word)
s = set('aeiou')
return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
s = set("aeiou")
ans, n = 0, len(word)
for i in range(n):
t = set()
for c in word[i:]:
if c not in s:
break
t.add(c)
ans += len(t) == 5
return ans
```
#### Java
@ -191,31 +202,4 @@ function countVowelSubstrings(word: string): number {
<!-- solution:end -->
<!-- solution:start -->
### Solution 2
<!-- tabs:start -->
#### Python3
```python
class Solution:
def countVowelSubstrings(self, word: str) -> int:
s = set('aeiou')
ans, n = 0, len(word)
for i in range(n):
t = set()
for c in word[i:]:
if c not in s:
break
t.add(c)
ans += len(t) == 5
return ans
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->

View File

@ -1,5 +1,12 @@
class Solution:
def countVowelSubstrings(self, word: str) -> int:
n = len(word)
s = set('aeiou')
return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
s = set("aeiou")
ans, n = 0, len(word)
for i in range(n):
t = set()
for c in word[i:]:
if c not in s:
break
t.add(c)
ans += len(t) == 5
return ans

View File

@ -1,12 +0,0 @@
class Solution:
def countVowelSubstrings(self, word: str) -> int:
s = set('aeiou')
ans, n = 0, len(word)
for i in range(n):
t = set()
for c in word[i:]:
if c not in s:
break
t.add(c)
ans += len(t) == 5
return ans