Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e721284

Browse files
committed
Add solution #1751
1 parent 6a67535 commit e721284

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,519 LeetCode solutions in JavaScript
1+
# 1,520 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1348,6 +1348,7 @@
13481348
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13491349
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13501350
1750|[Minimum Length of String After Deleting Similar Ends](./solutions/1750-minimum-length-of-string-after-deleting-similar-ends.js)|Medium|
1351+
1751|[Maximum Number of Events That Can Be Attended II](./solutions/1751-maximum-number-of-events-that-can-be-attended-ii.js)|Hard|
13511352
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
13521353
1764|[Form Array by Concatenating Subarrays of Another Array](./solutions/1764-form-array-by-concatenating-subarrays-of-another-array.js)|Medium|
13531354
1765|[Map of Highest Peak](./solutions/1765-map-of-highest-peak.js)|Medium|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* 1751. Maximum Number of Events That Can Be Attended II
3+
* https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended-ii/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event
7+
* starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value
8+
* of valuei. You are also given an integer k which represents the maximum number of events you can
9+
* attend.
10+
*
11+
* You can only attend one event at a time. If you choose to attend an event, you must attend the
12+
* entire event. Note that the end day is inclusive: that is, you cannot attend two events where
13+
* one of them starts and the other ends on the same day.
14+
*
15+
* Return the maximum sum of values that you can receive by attending events.
16+
*/
17+
18+
/**
19+
* @param {number[][]} events
20+
* @param {number} k
21+
* @return {number}
22+
*/
23+
var maxValue = function(events, k) {
24+
events.sort((a, b) => a[0] - b[0]);
25+
const n = events.length;
26+
const dp = Array.from({ length: k + 1 }, () => new Array(n + 1).fill(-1));
27+
28+
return maximize(0, k);
29+
30+
function findNext(index, end) {
31+
let left = index;
32+
let right = n;
33+
while (left < right) {
34+
const mid = Math.floor((left + right) / 2);
35+
if (events[mid][0] > end) {
36+
right = mid;
37+
} else {
38+
left = mid + 1;
39+
}
40+
}
41+
return left;
42+
}
43+
44+
function maximize(index, remaining) {
45+
if (index >= n || remaining === 0) return 0;
46+
if (dp[remaining][index] !== -1) return dp[remaining][index];
47+
48+
const nextIndex = findNext(index + 1, events[index][1]);
49+
const take = events[index][2] + maximize(nextIndex, remaining - 1);
50+
const skip = maximize(index + 1, remaining);
51+
52+
return dp[remaining][index] = Math.max(take, skip);
53+
}
54+
};

0 commit comments

Comments
 (0)