Skip to content

[pull] main from doocs:main #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54,506 changes: 27,303 additions & 27,203 deletions images/starcharts.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 44 additions & 7 deletions solution/2000-2099/2054.Two Best Non-Overlapping Events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 -->
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand All @@ -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
Expand Down Expand Up @@ -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 -->
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.