Skip to content

Commit 989cd7b

Browse files
committed
feat: add solution 1751. Maximum Number of Events That Can Be Attended II
1 parent a5ed46f commit 989cd7b

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# [1751. Maximum Number of Events That Can Be Attended II](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended-ii)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>You are given an array of <code>events</code> where <code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>, value<sub>i</sub>]</code>. The <code>i<sup>th</sup></code> event starts at <code>startDay<sub>i</sub></code><sub> </sub>and ends at <code>endDay<sub>i</sub></code>, and if you attend this event, you will receive a value of <code>value<sub>i</sub></code>. You are also given an integer <code>k</code> which represents the maximum number of events you can attend.</p>
6+
7+
<p>You can only attend one event at a time. If you choose to attend an event, you must attend the <strong>entire</strong> event. Note that the end day is <strong>inclusive</strong>: that is, you cannot attend two events where one of them starts and the other ends on the same day.</p>
8+
9+
<p>Return <em>the <strong>maximum sum</strong> of values that you can receive by attending events.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60048-pm.png" style="width: 400px; height: 103px;"></p>
15+
16+
<pre><strong>Input:</strong> events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
17+
<strong>Output:</strong> 7
18+
<strong>Explanation: </strong>Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60150-pm.png" style="width: 400px; height: 103px;"></p>
23+
24+
<pre><strong>Input:</strong> events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
25+
<strong>Output:</strong> 10
26+
<strong>Explanation:</strong> Choose event 2 for a total value of 10.
27+
Notice that you cannot attend any other event as they overlap, and that you do <strong>not</strong> have to attend k events.</pre>
28+
29+
<p><strong class="example">Example 3:</strong></p>
30+
31+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60703-pm.png" style="width: 400px; height: 126px;"></strong></p>
32+
33+
<pre><strong>Input:</strong> events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
34+
<strong>Output:</strong> 9
35+
<strong>Explanation:</strong> Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= k &lt;= events.length</code></li>
42+
<li><code>1 &lt;= k * events.length &lt;= 10<sup>6</sup></code></li>
43+
<li><code>1 &lt;= startDay<sub>i</sub> &lt;= endDay<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
44+
<li><code>1 &lt;= value<sub>i</sub> &lt;= 10<sup>6</sup></code></li>
45+
</ul>
46+
</div>
47+
48+
<p>&nbsp;</p>
49+
50+
## Solutions
51+
52+
**Solution: `Dynamic Programming + Binary Search`**
53+
54+
- Time complexity: <em>O(nlogn+nk)</em>
55+
- Space complexity: <em>O(nk)</em>
56+
57+
<p>&nbsp;</p>
58+
59+
### **JavaScript**
60+
61+
```js
62+
/**
63+
* @param {number[][]} events
64+
* @param {number} k
65+
* @return {number}
66+
*/
67+
const maxValue = function (events, k) {
68+
const n = events.length;
69+
const nextEventMap = new Map();
70+
const dp = Array.from({ length: n }, () => new Array(k + 1).fill(-1));
71+
72+
events.sort((a, b) => a[0] - b[0]);
73+
74+
const findNextEvent = target => {
75+
let left = 0;
76+
let right = n - 1;
77+
78+
while (left <= right) {
79+
const mid = Math.floor((left + right) / 2);
80+
const start = events[mid][0];
81+
82+
start > target ? (right = mid - 1) : (left = mid + 1);
83+
}
84+
85+
return left;
86+
};
87+
88+
for (let index = 0; index < n; index++) {
89+
const end = events[index][1];
90+
const nextEvent = findNextEvent(end);
91+
92+
nextEventMap.set(index, nextEvent);
93+
}
94+
95+
const attendEvent = (index, quota) => {
96+
if (index >= n || quota === 0) return 0;
97+
if (dp[index][quota] !== -1) return dp[index][quota];
98+
const skipEventValue = attendEvent(index + 1, quota);
99+
const value = events[index][2];
100+
const nextEvent = nextEventMap.get(index);
101+
const nextEventValue = value + attendEvent(nextEvent, quota - 1);
102+
const result = Math.max(skipEventValue, nextEventValue);
103+
104+
dp[index][quota] = result;
105+
106+
return result;
107+
};
108+
109+
return attendEvent(0, k);
110+
};
111+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number[][]} events
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const maxValue = function (events, k) {
7+
const n = events.length;
8+
const nextEventMap = new Map();
9+
const dp = Array.from({ length: n }, () => new Array(k + 1).fill(-1));
10+
11+
events.sort((a, b) => a[0] - b[0]);
12+
13+
const findNextEvent = target => {
14+
let left = 0;
15+
let right = n - 1;
16+
17+
while (left <= right) {
18+
const mid = Math.floor((left + right) / 2);
19+
const start = events[mid][0];
20+
21+
start > target ? (right = mid - 1) : (left = mid + 1);
22+
}
23+
24+
return left;
25+
};
26+
27+
for (let index = 0; index < n; index++) {
28+
const end = events[index][1];
29+
const nextEvent = findNextEvent(end);
30+
31+
nextEventMap.set(index, nextEvent);
32+
}
33+
34+
const attendEvent = (index, quota) => {
35+
if (index >= n || quota === 0) return 0;
36+
if (dp[index][quota] !== -1) return dp[index][quota];
37+
const skipEventValue = attendEvent(index + 1, quota);
38+
const value = events[index][2];
39+
const nextEvent = nextEventMap.get(index);
40+
const nextEventValue = value + attendEvent(nextEvent, quota - 1);
41+
const result = Math.max(skipEventValue, nextEventValue);
42+
43+
dp[index][quota] = result;
44+
45+
return result;
46+
};
47+
48+
return attendEvent(0, k);
49+
};

0 commit comments

Comments
 (0)