[LeetCode] Maximum Number of Events That Can Be Attended II

1751. Maximum Number of Events That Can Be Attended II

You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend.

You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day.

Return the maximum sum of values that you can receive by attending events.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
unordered_map<long, int> dp;
vector<int> bound;
int solution(vector<vector<int>>& events, int cur, int k) {
if(k == 1) return events[cur][2];
if(cur == events.size()) return 0;
long key = 1l * k * 1e7 + cur;
if(dp.count(key)) return dp[key];

dp[key] = events[cur][2];
vector<int> tmp = {events[cur][1]+1, INT_MIN,INT_MIN};
auto i = lower_bound(events.begin() + cur, events.end(), tmp) - events.begin();


int n = events.size();
for(; i < n; i++) {
dp[key] = max(dp[key], solution(events, i, k - 1) + events[cur][2]);
}

return dp[key];
}
public:
int maxValue(vector<vector<int>>& events, int k) {
int n = events.size();
sort(events.begin(),events.end());
int res = 0;
for(int i = 0; i < n; i++) {
res = max(res, solution(events,i,k));
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/23/PS/LeetCode/maximum-number-of-events-that-can-be-attended-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.