-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2054.java
More file actions
52 lines (42 loc) · 1.34 KB
/
LC2054.java
File metadata and controls
52 lines (42 loc) · 1.34 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
51
52
/*
* LC2054
*/
import java.util.*;
public class LC2054 {
public static int maxTwoEvents(int[][] events) {
// sort on end time
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
// sort on start time
Arrays.sort(events, (a, b) -> a[0] - b[0]);
int prevMax = 0;
int res = 0;
for (int[] event : events) {
// find previous max
while (!pq.isEmpty() && pq.peek()[1] < event[0]) {
prevMax = Math.max(prevMax, pq.peek()[2]);
pq.poll();
}
res = Math.max(res, prevMax + event[2]);
pq.offer(event);
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Array Size : ");
int size = sc.nextInt();
System.out.println();
int[][] arr = new int[size][size];
System.out.println("Enter The Matrix Events Array Elements : ");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.printf("[%d][%d] : ", i, j);
arr[i][j] = sc.nextInt();
}
}
System.out.println();
int ans = maxTwoEvents(arr);
System.out.println(ans);
sc.close();
}
}