-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution870.java
38 lines (32 loc) · 901 Bytes
/
Solution870.java
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
package algorithm.leetcode;
import java.util.Arrays;
import java.util.PriorityQueue;
/**
* @author: mayuan
* @desc: 优势洗牌
* @date: 2019/02/08
*/
public class Solution870 {
public int[] advantageCount(int[] A, int[] B) {
if (0 == A.length && 0 == B.length) {
return A;
}
Arrays.sort(A);
PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> b[1] - a[1]);
for (int i = 0; i < B.length; ++i) {
queue.add(new int[]{i, B[i]});
}
int[] ans = new int[A.length];
int low = 0, high = A.length - 1;
while (!queue.isEmpty()) {
int[] cur = queue.poll();
int idx = cur[0], val = cur[1];
if (A[high] > val) {
ans[idx] = A[high--];
} else {
ans[idx] = A[low++];
}
}
return ans;
}
}