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
Weak Characters Problem: Find Dominated CharactersInput: [[5,5],[6,3],[2,4],[1,6]] - Find characters dominated in both attack AND defense[5,5]Strong[6,3]Strong[2,4]WEAK[1,6]Strong5 > 2 AND 5 > 4[5,5] dominates [2,4]Attack: 5, Defense: 5Attack: 6, Defense: 3Attack: 2, Defense: 4Attack: 1, Defense: 6Output: 1 weak character
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
Asked in
Amazon 45 Google 38 Microsoft 32 Apple 28
67.5K Views
Medium-High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen