forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_870.java
42 lines (39 loc) · 1.27 KB
/
_870.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
39
40
41
42
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class _870 {
public static class Solution1 {
public int[] advantageCount(int[] A, int[] B) {
int[] result = new int[A.length];
Arrays.sort(A);
boolean[] used = new boolean[A.length];
for (int i = 0; i < A.length; i++) {
result[i] = findSmallestAdvantage(A, used, B[i]);
}
List<Integer> unused = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
if (!used[i]) {
unused.add(A[i]);
}
}
Iterator<Integer> iterator = unused.iterator();
for (int i = 0; i < A.length; i++) {
if (result[i] == -1) {
result[i] = iterator.next();
}
}
return result;
}
private int findSmallestAdvantage(int[] A, boolean[] used, int target) {
for (int i = 0; i < A.length; i++) {
if (!used[i] && A[i] > target) {
used[i] = true;
return A[i];
}
}
return -1;
}
}
}