-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay31.java
More file actions
49 lines (37 loc) · 1.4 KB
/
Day31.java
File metadata and controls
49 lines (37 loc) · 1.4 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
import java.util.ArrayList;
import java.util.PriorityQueue;
public class Day31 {
public ArrayList<Integer> solve(ArrayList<Integer> A, ArrayList<Integer> B, int C) {
int N = A.size();
A.sort((a, b) -> b - a);
B.sort((a, b) -> b - a);
PriorityQueue<Pair> maxHeap = new PriorityQueue<>((a, b) -> b.sum - a.sum);
maxHeap.offer(new Pair(0, 0, A.get(0) + B.get(0)));
boolean[][] visited = new boolean[N][N];
visited[0][0] = true;
ArrayList<Integer> result = new ArrayList<>();
for (int count = 0; count < C; count++) {
Pair current = maxHeap.poll();
result.add(current.sum);
if (current.i + 1 < N && !visited[current.i + 1][current.j]) {
maxHeap.offer(new Pair(current.i + 1, current.j, A.get(current.i + 1) + B.get(current.j)));
visited[current.i + 1][current.j] = true;
}
if (current.j + 1 < N && !visited[current.i][current.j + 1]) {
maxHeap.offer(new Pair(current.i, current.j + 1, A.get(current.i) + B.get(current.j + 1)));
visited[current.i][current.j + 1] = true;
}
}
return result;
}
static class Pair {
int i;
int j;
int sum;
Pair(int i, int j, int sum) {
this.i = i;
this.j = j;
this.sum = sum;
}
}
}