Skip to content

Commit 33c42ad

Browse files
committed
Add solution #2054
1 parent d19a16e commit 33c42ad

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,7 @@
18621862
2050|[Parallel Courses III](./solutions/2050-parallel-courses-iii.js)|Hard|
18631863
2052|[Minimum Cost to Separate Sentence Into Rows](./solutions/2052-minimum-cost-to-separate-sentence-into-rows.js)|Medium|
18641864
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
1865+
2054|[Two Best Non-Overlapping Events](./solutions/2054-two-best-non-overlapping-events.js)|Medium|
18651866
2055|[Plates Between Candles](./solutions/2055-plates-between-candles.js)|Medium|
18661867
2056|[Number of Valid Move Combinations On Chessboard](./solutions/2056-number-of-valid-move-combinations-on-chessboard.js)|Hard|
18671868
2057|[Smallest Index With Equal Value](./solutions/2057-smallest-index-with-equal-value.js)|Easy|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* 2054. Two Best Non-Overlapping Events
3+
* https://leetcode.com/problems/two-best-non-overlapping-events/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed 2D integer array of events where
7+
* events[i] = [startTimei, endTimei, valuei]. The ith event starts at
8+
* startTimei and ends at endTimei, and if you attend this event, you will
9+
* receive a value of valuei. You can choose at most two non-overlapping
10+
* events to attend such that the sum of their values is maximized.
11+
*
12+
* Return this maximum sum.
13+
*
14+
* Note that the start time and end time is inclusive: that is, you cannot
15+
* attend two events where one of them starts and the other ends at the
16+
* same time. More specifically, if you attend an event with end time t,
17+
* the next event must start at or after t + 1.
18+
*/
19+
20+
/**
21+
* @param {number[][]} events
22+
* @return {number}
23+
*/
24+
var maxTwoEvents = function(events) {
25+
events.sort((a, b) => a[1] - b[1]);
26+
27+
const maxValueUpTo = new Array(events.length);
28+
maxValueUpTo[0] = events[0][2];
29+
30+
for (let i = 1; i < events.length; i++) {
31+
maxValueUpTo[i] = Math.max(maxValueUpTo[i - 1], events[i][2]);
32+
}
33+
34+
let result = 0;
35+
for (let i = 0; i < events.length; i++) {
36+
const [start, end, value] = events[i];
37+
result = Math.max(result, value);
38+
39+
let left = 0;
40+
let right = i - 1;
41+
let bestIndex = -1;
42+
while (left <= right) {
43+
const mid = Math.floor((left + right) / 2);
44+
if (events[mid][1] < start) {
45+
bestIndex = mid;
46+
left = mid + 1;
47+
} else {
48+
right = mid - 1;
49+
}
50+
}
51+
52+
if (bestIndex !== -1) {
53+
result = Math.max(result, value + maxValueUpTo[bestIndex]);
54+
}
55+
}
56+
57+
return result;
58+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy