|
68 | 68 |
|
69 | 69 | <!-- solution:start -->
|
70 | 70 |
|
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. |
72 | 78 |
|
73 | 79 | <!-- tabs:start -->
|
74 | 80 |
|
@@ -130,22 +136,27 @@ class Solution {
|
130 | 136 | class Solution {
|
131 | 137 | public:
|
132 | 138 | int maxTwoEvents(vector<vector<int>>& events) {
|
133 |
| - sort(events.begin(), events.end()); |
| 139 | + ranges::sort(events); |
134 | 140 | int n = events.size();
|
135 | 141 | 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 | + } |
137 | 145 | int ans = 0;
|
138 |
| - for (auto& e : events) { |
| 146 | + for (const auto& e : events) { |
139 | 147 | int v = e[2];
|
140 | 148 | int left = 0, right = n;
|
141 | 149 | while (left < right) {
|
142 | 150 | int mid = (left + right) >> 1;
|
143 |
| - if (events[mid][0] > e[1]) |
| 151 | + if (events[mid][0] > e[1]) { |
144 | 152 | right = mid;
|
145 |
| - else |
| 153 | + } else { |
146 | 154 | left = mid + 1;
|
| 155 | + } |
| 156 | + } |
| 157 | + if (left < n) { |
| 158 | + v += f[left]; |
147 | 159 | }
|
148 |
| - if (left < n) v += f[left]; |
149 | 160 | ans = max(ans, v);
|
150 | 161 | }
|
151 | 162 | return ans;
|
@@ -186,6 +197,34 @@ func maxTwoEvents(events [][]int) int {
|
186 | 197 | }
|
187 | 198 | ```
|
188 | 199 |
|
| 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 | + |
189 | 228 | <!-- tabs:end -->
|
190 | 229 |
|
191 | 230 | <!-- solution:end -->
|
|
0 commit comments