Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: properties = [[5,5],[6,3],[3,6]] Output: 0 Explanation: No character has strictly greater attack and defense than the other.
Example 2:
Input: properties = [[2,2],[3,3]] Output: 1 Explanation: The first character is weak because the second character has a strictly greater attack and defense.
Example 3:
Input: properties = [[1,5],[10,4],[4,3]] Output: 1 Explanation: The third character is weak because the second character has a strictly greater attack and defense.
Constraints:
2 <= properties.length <= 105properties[i].length == 21 <= attacki, defensei <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Stack · Greedy
[[5,5],[6,3],[3,6]]
[[2,2],[3,3]]
[[1,5],[10,4],[4,3]]
russian-doll-envelopes)maximum-height-by-stacking-cuboids)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1996: The Number of Weak Characters in the Game
class Solution {
public int numberOfWeakCharacters(int[][] properties) {
Arrays.sort(properties, (a, b) -> b[0] - a[0] == 0 ? a[1] - b[1] : b[0] - a[0]);
int ans = 0, mx = 0;
for (var x : properties) {
if (x[1] < mx) {
++ans;
}
mx = Math.max(mx, x[1]);
}
return ans;
}
}
// Accepted solution for LeetCode #1996: The Number of Weak Characters in the Game
func numberOfWeakCharacters(properties [][]int) (ans int) {
sort.Slice(properties, func(i, j int) bool {
a, b := properties[i], properties[j]
if a[0] == b[0] {
return a[1] < b[1]
}
return a[0] > b[0]
})
mx := 0
for _, x := range properties {
if x[1] < mx {
ans++
} else {
mx = x[1]
}
}
return
}
# Accepted solution for LeetCode #1996: The Number of Weak Characters in the Game
class Solution:
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
properties.sort(key=lambda x: (-x[0], x[1]))
ans = mx = 0
for _, x in properties:
ans += x < mx
mx = max(mx, x)
return ans
// Accepted solution for LeetCode #1996: The Number of Weak Characters in the Game
/**
* [1996] The Number of Weak Characters in the Game
*
* 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 i^th 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.
*
* Example 1:
*
* Input: properties = [[5,5],[6,3],[3,6]]
* Output: 0
* Explanation: No character has strictly greater attack and defense than the other.
*
* Example 2:
*
* Input: properties = [[2,2],[3,3]]
* Output: 1
* Explanation: The first character is weak because the second character has a strictly greater attack and defense.
*
* Example 3:
*
* Input: properties = [[1,5],[10,4],[4,3]]
* Output: 1
* Explanation: The third character is weak because the second character has a strictly greater attack and defense.
*
*
* Constraints:
*
* 2 <= properties.length <= 10^5
* properties[i].length == 2
* 1 <= attacki, defensei <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/
// discuss: https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn number_of_weak_characters(properties: Vec<Vec<i32>>) -> i32 {
let mut properties = properties;
properties.sort_unstable_by_key(|p| (-p[0], p[1]));
properties
.iter()
.fold((0, -1), |(count, max_def), prop| {
if max_def > prop[1] {
(count + 1, max_def)
} else {
(count, prop[1])
}
})
.0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1996_example_1() {
let properties = vec![vec![5, 5], vec![6, 3], vec![3, 6]];
let result = 0;
assert_eq!(Solution::number_of_weak_characters(properties), result);
}
#[test]
fn test_1996_example_2() {
let properties = vec![vec![2, 2], vec![3, 3]];
let result = 1;
assert_eq!(Solution::number_of_weak_characters(properties), result);
}
#[test]
fn test_1996_example_3() {
let properties = vec![vec![1, 5], vec![10, 4], vec![4, 3]];
let result = 1;
assert_eq!(Solution::number_of_weak_characters(properties), result);
}
}
// Accepted solution for LeetCode #1996: The Number of Weak Characters in the Game
function numberOfWeakCharacters(properties: number[][]): number {
properties.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
let ans = 0;
let mx = 0;
for (const [, x] of properties) {
if (x < mx) {
ans++;
} else {
mx = x;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan left (or right) to find the next greater/smaller element. The inner scan can visit up to n elements per outer iteration, giving O(n²) total comparisons. No extra space needed beyond loop variables.
Each element is pushed onto the stack at most once and popped at most once, giving 2n total operations = O(n). The stack itself holds at most n elements in the worst case. The key insight: amortized O(1) per element despite the inner while-loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.