The Number of Weak Characters in the Game - Problem

๐ŸŽฎ The Number of Weak Characters in the Game

Welcome to an epic RPG battle arena! You're analyzing character statistics to determine which heroes need power-ups.

You have a collection of game characters, each with two crucial combat stats: attack and defense. Your mission is to identify how many characters are considered weak in the current meta.

A character is weak if there exists at least one other character who completely dominates them - meaning that other character has both higher attack and higher defense values.

Input: A 2D array properties where properties[i] = [attack_i, defense_i] represents the combat stats of the i-th character.

Output: Return the total count of weak characters who are completely outclassed by at least one other character.

Example: If Character A has [5, 5] and Character B has [6, 6], then Character A is weak because Character B dominates in both stats.

Input & Output

example_1.py โ€” Basic Characters
$ Input: properties = [[2,2],[3,3]]
โ€บ Output: 1
๐Ÿ’ก Note: The first character [2,2] is weak because the second character [3,3] has both higher attack (3 > 2) and higher defense (3 > 2). The second character [3,3] is not weak since no other character dominates it in both stats.
example_2.py โ€” Multiple Characters
$ Input: properties = [[5,5],[6,3],[3,6]]
โ€บ Output: 0
๐Ÿ’ก Note: No character is weak. Character [5,5] vs [6,3]: 6>5 but 3<5. Character [5,5] vs [3,6]: 3<5 but 6>5. Each character has at least one stat that's not strictly dominated by others.
example_3.py โ€” Complex Scenario
$ Input: properties = [[1,5],[10,4],[4,3]]
โ€บ Output: 1
๐Ÿ’ก Note: Character [4,3] is weak because character [10,4] has both higher attack (10 > 4) and higher defense (4 > 3). Characters [1,5] and [10,4] don't dominate each other completely.

Constraints

  • 2 โ‰ค properties.length โ‰ค 105
  • properties[i].length == 2
  • 1 โ‰ค attacki, defensei โ‰ค 105
  • All characters have positive integer stats

Visualization

Tap to expand
๐ŸŽฎ Character Weakness AnalysisATK:10DEF:2ChampionATK:8DEF:7StrongATK:5DEF:3Weak!ATK:3DEF:9SafeDominates!8>5 AND 7>3๐Ÿง  Algorithm Insight1. Sort by Attack (descending) โ†’ Process strongest first2. Use Stack to track maximum Defense seen so far3. If stack has higher defense, current character is weak!โšก Time: O(n log n) | Space: O(n)
Understanding the Visualization
1
Arrange by Attack Power
Sort all characters by attack strength (highest first)
2
Track Defense Champions
Use a stack to remember the strongest defenders seen so far
3
Identify the Weak
Any character facing a stronger defender with higher attack is weak
Key Takeaway
๐ŸŽฏ Key Insight: By sorting characters by attack power and using a monotonic stack to track defense values, we can efficiently identify weak characters without comparing everyone to everyone!
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
41.2K Views
High Frequency
~18 min Avg. Time
1.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