Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5c189bc

Browse files
authoredFeb 7, 2021
Create 1751-maximum-number-of-events-that-can-be-attended-ii.js
1 parent ac4d7f2 commit 5c189bc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
 
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[][]} events
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const maxValue = function(events, k) {
7+
8+
cache = new Map();
9+
events.sort((a,b) => a[0] - b[0]);
10+
return dfs(events, 0, -1, k);
11+
12+
function dfs(events, idx, end, k){
13+
let key = idx + "," + end + "," + k;
14+
if(cache.has(key)){
15+
return cache.get(key);
16+
}
17+
if(idx >= events.length){
18+
return 0;
19+
}
20+
if(k == 0) {
21+
return 0;
22+
}
23+
let max = 0;
24+
if(events[idx][0] > end){
25+
max = Math.max(max, dfs(events, idx+1, events[idx][1], k-1) + events[idx][2]);
26+
}
27+
28+
max = Math.max(max, dfs(events, idx+1, end, k));
29+
cache.set(key, max);
30+
return max;
31+
}
32+
};

0 commit comments

Comments
 (0)
Please sign in to comment.