Imagine a video game where characters have two primary attributes: attack and defense. A character is considered "weak" if there exists another character in the game who has both strictly greater attack and strictly greater defense. Given a list of characters with their attributes, your task is to count how many characters are weak.
This the Number of Weak Characters in the Game interview question is a popular challenge at Pinterest and Google. It tests your ability to optimize a comparison-heavy problem. A naive O(n^2) approach checking every pair of characters is too slow for large inputs. The problem evaluates your understanding of sorting and greedy strategies, specifically how to arrange data so that a single pass can identify all weak elements.
This problem utilizes the Array, Monotonic Stack, Sorting, Stack, Greedy interview pattern.
Characters: [[5, 5], [6, 3], [3, 6]].
The most common mistake in "The Number of Weak Characters in the Game coding problem" is sorting both attributes in the same direction (both descending). This makes it difficult to handle characters with the same attack. Another mistake is failing to realize that "strictly greater" applies to both attributes, which is why the sorting trick for equal attacks is so important.
Sorting is often the first step in optimizing a search or comparison problem. When a problem has two attributes, try sorting by one and observing the behavior of the other. The "descending/ascending" sort combination is a powerful pattern for problems involving 2D dominance.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Max Chunks To Make Sorted | Medium | Solve | |
| Max Chunks To Make Sorted II | Hard | Solve | |
| Shortest Unsorted Continuous Subarray | Medium | Solve | |
| Make Array Non-decreasing | Medium | Solve | |
| Maximum Length of Semi-Decreasing Subarrays | Medium | Solve |