diff --git a/images/starcharts.svg b/images/starcharts.svg index a3d28a13f2774..649b261845164 100644 --- a/images/starcharts.svg +++ b/images/starcharts.svg @@ -1,4 +1,4 @@ - + \n2018-09-252019-07-132020-04-292021-02-142021-12-032022-09-202023-07-082024-04-252025-02-10Time2019-07-142020-05-012021-02-172021-12-072022-09-252023-07-142024-05-022025-02-18Time0420033000Stargazers \ No newline at end of file +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 12 +L 948 11 +L 948 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 949 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11 +L 950 11" style="stroke-width:2;stroke:rgba(129,199,239,1.0);fill:none"/> \ No newline at end of file diff --git a/solution/2000-2099/2054.Two Best Non-Overlapping Events/README.md b/solution/2000-2099/2054.Two Best Non-Overlapping Events/README.md index b6bf5fbdce5cb..22efaf46225b6 100644 --- a/solution/2000-2099/2054.Two Best Non-Overlapping Events/README.md +++ b/solution/2000-2099/2054.Two Best Non-Overlapping Events/README.md @@ -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$ 为活动的数量。 @@ -137,22 +141,27 @@ class Solution { class Solution { public: int maxTwoEvents(vector>& events) { - sort(events.begin(), events.end()); + ranges::sort(events); int n = events.size(); vector 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; +} +``` + diff --git a/solution/2000-2099/2054.Two Best Non-Overlapping Events/README_EN.md b/solution/2000-2099/2054.Two Best Non-Overlapping Events/README_EN.md index ae8de3ba6d653..b2f2af73217a3 100644 --- a/solution/2000-2099/2054.Two Best Non-Overlapping Events/README_EN.md +++ b/solution/2000-2099/2054.Two Best Non-Overlapping Events/README_EN.md @@ -68,7 +68,13 @@ tags: -### 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. @@ -130,22 +136,27 @@ class Solution { class Solution { public: int maxTwoEvents(vector>& events) { - sort(events.begin(), events.end()); + ranges::sort(events); int n = events.size(); vector 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; +} +``` + diff --git a/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp b/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp index 8e9ee1c17e6aa..05edee865ad83 100644 --- a/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp +++ b/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp @@ -1,24 +1,29 @@ class Solution { public: int maxTwoEvents(vector>& events) { - sort(events.begin(), events.end()); + ranges::sort(events); int n = events.size(); vector 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; } -}; \ No newline at end of file +}; diff --git a/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.ts b/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.ts new file mode 100644 index 0000000000000..8c7709f8306e2 --- /dev/null +++ b/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.ts @@ -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; +} diff --git a/solution/2000-2099/2062.Count Vowel Substrings of a String/README.md b/solution/2000-2099/2062.Count Vowel Substrings of a String/README.md index 5239fdba01444..fc67911dc310c 100644 --- a/solution/2000-2099/2062.Count Vowel Substrings of a String/README.md +++ b/solution/2000-2099/2062.Count Vowel Substrings of a String/README.md @@ -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 { - - -### 方法二 - - - -#### 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 -``` - - - - - diff --git a/solution/2000-2099/2062.Count Vowel Substrings of a String/README_EN.md b/solution/2000-2099/2062.Count Vowel Substrings of a String/README_EN.md index 03631871aa52a..1d0b9c83e8700 100644 --- a/solution/2000-2099/2062.Count Vowel Substrings of a String/README_EN.md +++ b/solution/2000-2099/2062.Count Vowel Substrings of a String/README_EN.md @@ -73,7 +73,11 @@ tags: -### 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. @@ -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 2 - - - -#### 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 -``` - - - - - diff --git a/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.py b/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.py index 980872cdd6409..0b0014195b013 100644 --- a/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.py +++ b/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.py @@ -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 diff --git a/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution2.py b/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution2.py deleted file mode 100644 index 1716ea8343c95..0000000000000 --- a/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution2.py +++ /dev/null @@ -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