#2225
Medium Algorithms Find players with zero or one losses
Array Hash Table Sorting Counting
72.5% acceptance
Feb 25, 2026
2266
159
You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
Return a list answer of size 2 where:
answer[0] is a list of all players that have not lost any matches.
answer[1] is a list of all players that have lost exactly one match.
The values in the two lists should be returned in increasing order.
Note:
You should only consider the players that have played at least one match.
The testcases will be generated such that no two matches will have the same outcome.
Solution
Rust
Time O(n log n)
Space O(n)
use std::collections::HashMap;
impl Solution {
pub fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut losses: HashMap<i32, i32> = HashMap::new();
for m in &matches {
let (w, l) = (m[0], m[1]);
losses.entry(w).or_insert(0);
*losses.entry(l).or_insert(0) += 1;
}
let mut zero_losses: Vec<i32> = losses.iter().filter(|&(_, &v)| v == 0).map(|(&k, _)| k).collect();
let mut one_loss: Vec<i32> = losses.iter().filter(|&(_, &v)| v == 1).map(|(&k, _)| k).collect();
zero_losses.sort();
one_loss.sort();
vec![zero_losses, one_loss]
}
}