The Number of Weak Characters in the Game - Problem
You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense.
You are given a 2D integer array properties where properties[i] = [attacki, defensei] represents the properties of the ith character in the game.
A character is said to be weak if any other character has both attack and defense levels strictly greater than this character's attack and defense levels. More formally, a character i is said to be weak if there exists another character j where attackj > attacki and defensej > defensei.
Return the number of weak characters.
Input & Output
Example 1 — Mixed Characters
$
Input:
properties = [[5,5],[6,3],[2,4],[1,6]]
›
Output:
1
💡 Note:
Character [2,4] is weak because [5,5] has both higher attack (5>2) and higher defense (5>4). All other characters are not dominated by any single character.
Example 2 — All Strong Characters
$
Input:
properties = [[2,2],[3,3]]
›
Output:
1
💡 Note:
Character [2,2] is weak because [3,3] has both higher attack (3>2) and higher defense (3>2). Character [3,3] is not weak.
Example 3 — No Weak Characters
$
Input:
properties = [[1,5],[10,4],[4,3]]
›
Output:
0
💡 Note:
No character dominates any other in both attack AND defense: [1,5] has highest defense, [10,4] has highest attack, [4,3] is not dominated by either.
Constraints
- 2 ≤ properties.length ≤ 105
- properties[i].length == 2
- 1 ≤ attacki, defensei ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Characters
Each character has [attack, defense] values
2
Find Domination
Check if any character dominates another in both stats
3
Count Weak
Return number of characters that are dominated
Key Takeaway
🎯 Key Insight: Sort by attack to eliminate one comparison dimension, then efficiently check defense values
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code