You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -72,7 +72,23 @@ Notice that you cannot attend any other event as they overlap, and that you do <
72
72
73
73
<!-- solution:start -->
74
74
75
-
### Solution 1
75
+
### Solution 1: Memoization + Binary Search
76
+
77
+
First, we sort the events by their start time in ascending order. Then, we define a function $\text{dfs}(i, k)$, which represents the maximum total value achievable by attending at most $k$ events starting from the $i$-th event. The answer is $\text{dfs}(0, k)$.
78
+
79
+
The calculation process of the function $\text{dfs}(i, k)$ is as follows:
80
+
81
+
If we do not attend the $i$-th event, the maximum value is $\text{dfs}(i + 1, k)$. If we attend the $i$-th event, we can use binary search to find the first event whose start time is greater than the end time of the $i$-th event, denoted as $j$. Then, the maximum value is $\text{dfs}(j, k - 1) + \text{value}[i]$. We take the maximum of the two options:
Here, $j$ is the index of the first event whose start time is greater than the end time of the $i$-th event, which can be found using binary search.
88
+
89
+
Since the calculation of $\text{dfs}(i, k)$ involves calls to $\text{dfs}(i + 1, k)$ and $\text{dfs}(j, k - 1)$, we can use memoization to store the computed values and avoid redundant calculations.
90
+
91
+
The time complexity is $O(n \times \log n + n \times k)$, and the space complexity is $O(n \times k)$, where $n$ is the number of events.
76
92
77
93
<!-- tabs:start -->
78
94
@@ -145,23 +161,29 @@ class Solution {
145
161
classSolution {
146
162
public:
147
163
int maxValue(vector<vector<int>>& events, int k) {
148
-
sort(events.begin(), events.end());
164
+
ranges::sort(events);
149
165
int n = events.size();
150
166
int f[n][k + 1];
151
167
memset(f, 0, sizeof(f));
152
-
function<int(int, int)> dfs = [&](int i, int k) -> int {
168
+
auto dfs = [&](this auto&& dfs, int i, int k) -> int {
153
169
if (i >= n || k <= 0) {
154
170
return 0;
155
171
}
156
172
if (f[i][k] > 0) {
157
173
return f[i][k];
158
174
}
175
+
159
176
int ed = events[i][1], val = events[i][2];
160
177
vector<int> t = {ed};
161
-
int p = upper_bound(events.begin() + i + 1, events.end(), t, [](const auto& a, const auto& b) { return a[0] < b[0]; }) - events.begin();
178
+
179
+
int p = upper_bound(events.begin() + i + 1, events.end(), t,
180
+
[](const auto& a, const auto& b) { return a[0] < b[0]; })
We can convert the memoization approach in Solution 1 to dynamic programming.
267
+
268
+
First, sort the events, this time by end time in ascending order. Then define $f[i][j]$ as the maximum total value by attending at most $j$ events among the first $i$ events. The answer is $f[n][k]$.
269
+
270
+
For the $i$-th event, we can choose to attend it or not. If we do not attend, the maximum value is $f[i][j]$. If we attend, we can use binary search to find the last event whose end time is less than the start time of the $i$-th event, denoted as $h$. Then the maximum value is $f[h + 1][j - 1] + \text{value}[i]$. We take the maximum of the two options:
Here, $h$ is the last event whose end time is less than the start time of the $i$-th event, which can be found by binary search.
277
+
278
+
The time complexity is $O(n \times \log n + n \times k)$, and the space complexity is $O(n \times k)$, where $n$ is the number of events.
279
+
280
+
Related problems:
281
+
282
+
-[1235. Maximum Profit in Job Scheduling](https://github.com/doocs/leetcode/blob/main/solution/1200-1299/1235.Maximum%20Profit%20in%20Job%20Scheduling/README_EN.md)
283
+
-[2008. Maximum Earnings From Taxi](https://github.com/doocs/leetcode/blob/main/solution/2000-2099/2008.Maximum%20Earnings%20From%20Taxi/README_EN.md)
235
284
236
285
<!-- tabs:start -->
237
286
@@ -327,6 +376,37 @@ func maxValue(events [][]int, k int) int {
327
376
}
328
377
```
329
378
379
+
#### TypeScript
380
+
381
+
```ts
382
+
function maxValue(events:number[][], k:number):number {
0 commit comments