-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
50 lines (39 loc) · 1.67 KB
/
Solution.java
File metadata and controls
50 lines (39 loc) · 1.67 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package BinarySearch;
import java.util.Arrays;
class Solution {
public int maxTwoEvents(int[][] events) {
int n = events.length;
// Step 1: Sort the events by their start time
Arrays.sort(events, (a, b) -> a[0] - b[0]);
// Step 2: Create the suffix array for the maximum event value from each event onward
int[] suffixMax = new int[n];
suffixMax[n - 1] = events[n - 1][2]; // Initialize the last event's value
// Populate the suffixMax array
for (int i = n - 2; i >= 0; --i) {
suffixMax[i] = Math.max(events[i][2], suffixMax[i + 1]);
}
// Step 3: For each event, find the next event that starts after it ends
int maxSum = 0;
for (int i = 0; i < n; ++i) {
int left = i + 1, right = n - 1;
int nextEventIndex = -1;
// Perform binary search to find the next non-overlapping event
while (left <= right) {
int mid = left + (right - left) / 2;
if (events[mid][0] > events[i][1]) {
nextEventIndex = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
// If a valid next event is found, update the max sum
if (nextEventIndex != -1) {
maxSum = Math.max(maxSum, events[i][2] + suffixMax[nextEventIndex]);
}
// Also consider the case where we take only the current event
maxSum = Math.max(maxSum, events[i][2]);
}
return maxSum;
}
}