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 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:
Example 1:
Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]] Output: [[1,2,10],[4,5,7,8]] Explanation: Players 1, 2, and 10 have not lost any matches. Players 4, 5, 7, and 8 each have lost one match. Players 3, 6, and 9 each have lost two matches. Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
Example 2:
Input: matches = [[2,3],[1,3],[5,4],[6,4]] Output: [[1,2,5,6],[]] Explanation: Players 1, 2, 5, and 6 have not lost any matches. Players 3 and 4 each have lost two matches. Thus, answer[0] = [1,2,5,6] and answer[1] = [].
Constraints:
1 <= matches.length <= 105matches[i].length == 21 <= winneri, loseri <= 105winneri != loserimatches[i] are unique.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
[[2,3],[1,3],[5,4],[6,4]]
lowest-common-ancestor-of-a-binary-tree)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2225: Find Players With Zero or One Losses
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer, Integer> cnt = new HashMap<>();
for (var e : matches) {
cnt.putIfAbsent(e[0], 0);
cnt.merge(e[1], 1, Integer::sum);
}
List<List<Integer>> ans = List.of(new ArrayList<>(), new ArrayList<>());
for (var e : cnt.entrySet()) {
if (e.getValue() < 2) {
ans.get(e.getValue()).add(e.getKey());
}
}
Collections.sort(ans.get(0));
Collections.sort(ans.get(1));
return ans;
}
}
// Accepted solution for LeetCode #2225: Find Players With Zero or One Losses
func findWinners(matches [][]int) [][]int {
cnt := map[int]int{}
for _, e := range matches {
if _, ok := cnt[e[0]]; !ok {
cnt[e[0]] = 0
}
cnt[e[1]]++
}
ans := make([][]int, 2)
for x, v := range cnt {
if v < 2 {
ans[v] = append(ans[v], x)
}
}
sort.Ints(ans[0])
sort.Ints(ans[1])
return ans
}
# Accepted solution for LeetCode #2225: Find Players With Zero or One Losses
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
cnt = Counter()
for winner, loser in matches:
if winner not in cnt:
cnt[winner] = 0
cnt[loser] += 1
ans = [[], []]
for x, v in sorted(cnt.items()):
if v < 2:
ans[v].append(x)
return ans
// Accepted solution for LeetCode #2225: Find Players With Zero or One Losses
/**
* [2225] Find Players With Zero or One Losses
*
* 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.
*
*
* Example 1:
*
* Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
* Output: [[1,2,10],[4,5,7,8]]
* Explanation:
* Players 1, 2, and 10 have not lost any matches.
* Players 4, 5, 7, and 8 each have lost one match.
* Players 3, 6, and 9 each have lost two matches.
* Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
*
* Example 2:
*
* Input: matches = [[2,3],[1,3],[5,4],[6,4]]
* Output: [[1,2,5,6],[]]
* Explanation:
* Players 1, 2, 5, and 6 have not lost any matches.
* Players 3 and 4 each have lost two matches.
* Thus, answer[0] = [1,2,5,6] and answer[1] = [].
*
*
* Constraints:
*
* 1 <= matches.length <= 10^5
* matches[i].length == 2
* 1 <= winneri, loseri <= 10^5
* winneri != loseri
* All matches[i] are unique.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/find-players-with-zero-or-one-losses/
// discuss: https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2225_example_1() {
let matches = vec![
vec![1, 3],
vec![2, 3],
vec![3, 6],
vec![5, 6],
vec![5, 7],
vec![4, 5],
vec![4, 8],
vec![4, 9],
vec![10, 4],
vec![10, 9],
];
let result = vec![vec![1, 2, 10], vec![4, 5, 7, 8]];
assert_eq!(Solution::find_winners(matches), result);
}
#[test]
#[ignore]
fn test_2225_example_2() {
let matches = vec![vec![2, 3], vec![1, 3], vec![5, 4], vec![6, 4]];
let result = vec![vec![1, 2, 5, 6], vec![]];
assert_eq!(Solution::find_winners(matches), result);
}
}
// Accepted solution for LeetCode #2225: Find Players With Zero or One Losses
function findWinners(matches: number[][]): number[][] {
const cnt: Map<number, number> = new Map();
for (const [winner, loser] of matches) {
if (!cnt.has(winner)) {
cnt.set(winner, 0);
}
cnt.set(loser, (cnt.get(loser) || 0) + 1);
}
const ans: number[][] = [[], []];
for (const [x, v] of cnt) {
if (v < 2) {
ans[v].push(x);
}
}
ans[0].sort((a, b) => a - b);
ans[1].sort((a, b) => a - b);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.