Skip to content

Commit 71afb1e

Browse files
committed
Add Greedy/1996_The_Number_of_Weak_Characters_in_the_Game.java
1 parent e98abf2 commit 71afb1e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int numberOfWeakCharacters(int[][] properties) {
3+
Arrays.sort(properties, (p1, p2) -> p1[0] == p2[0] ? p2[1] - p1[1] : p1[0] - p2[0]);
4+
5+
int result = 0, highestDefence = Integer.MIN_VALUE;
6+
7+
for (int i = properties.length - 1; i >= 0; i--) {
8+
int defence = properties[i][1];
9+
highestDefence = Math.max(highestDefence, defence);
10+
11+
if (defence < highestDefence) {
12+
++result;
13+
}
14+
}
15+
16+
return result;
17+
18+
}
19+
}

0 commit comments

Comments
 (0)