# 游戏中弱角色的数量
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
class Solution {
public static void main(String[] args) {
Object res = new Solution().numberOfWeakCharacters(new int[][]{
{1, 5}, {1, 4}, {4, 3}
});
System.out.println(res);
}
public int numberOfWeakCharacters(int[][] properties) {
Arrays.sort(properties, (o1, o2) -> o1[0] == o2[0] ? (o1[1] - o2[1]) : (o2[0] - o1[0]));
int maxDef = 0;
int ans = 0;
for (int[] p : properties) {
if (p[1] < maxDef) {
ans++;
} else {
maxDef = p[1];
}
}
return ans;
}
@Deprecated
public int numberOfWeakCharacters_Deprecated(int[][] properties) {
HashSet<int[]> weaker = new HashSet<>();
for (int i = 0; i < properties.length - 1; i++) {
for (int j = i + 1; j < properties.length; j++) {
if (properties[i][0] > properties[j][0] && properties[i][1] > properties[j][1]) {
weaker.add(properties[j]);
} else {
if (properties[i][0] < properties[j][0] && properties[i][1] < properties[j][1]) {
weaker.add(properties[i]);
}
}
}
}
return weaker.size();
}
}
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
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