-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode2054.java
More file actions
68 lines (61 loc) · 2.32 KB
/
LeetCode2054.java
File metadata and controls
68 lines (61 loc) · 2.32 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class LeetCode2054 {
public static void main(String[] args) {
// 输入:events = [[1,3,2],[4,5,2],[2,4,3]]
// 输出:4
System.out.println(new Solution2054_2().maxTwoEvents(new int[][] { { 1, 3, 2 }, { 4, 5, 2 }, { 2, 4, 3 } }));
// 输入:events = [[1,3,2],[4,5,2],[1,5,5]]
// 输出:5
System.out.println(new Solution2054_2().maxTwoEvents(new int[][] { { 1, 3, 2 }, { 4, 5, 2 }, { 1, 5, 5 } }));
// 输入:events = [[1,5,3],[1,5,1],[6,6,5]]
// 输出:8
System.out.println(new Solution2054_2().maxTwoEvents(new int[][] { { 1, 5, 3 }, { 1, 5, 1 }, { 6, 6, 5 } }));
// 输入:events = [[10,83,53],[63,87,45],[97,100,32],[51,61,16]]
// 输出:85
System.out.println(new Solution2054_2()
.maxTwoEvents(new int[][] { { 10, 83, 53 }, { 63, 87, 45 }, { 97, 100, 32 }, { 51, 61, 16 } }));
}
}
class Solution2054_1 {
// 该解法会超时
public int maxTwoEvents(int[][] events) {
Arrays.sort(events, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
// System.out.println(Arrays.deepToString(events));
int ans = 0;
for (int i = 0; i < events.length; i++) {
ans = Math.max(ans, events[i][2]);
}
for (int i = 0; i < events.length; i++) {
for (int j = events.length - 1; j >= 0; j--) {
if (events[i][1] >= events[j][0]) {
break;
}
ans = Math.max(ans, events[i][2] + events[j][2]);
}
}
return ans;
}
}
class Solution2054_2 {
public int maxTwoEvents(int[][] events) {
Arrays.sort(events, (a, b) -> (a[0] - b[0]));
// System.out.println(Arrays.deepToString(events));
PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> (a[1] - b[1]));
int ans = 0;
int max = 0;
for (int[] e : events) {
while (queue.size() > 0 && queue.peek()[1] < e[0]) {
max = Math.max(max, queue.poll()[2]);
}
ans = Math.max(ans, max + e[2]);
queue.offer(e);
}
return ans;
}
}