Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save coderRohan123/cc504decf6102749aa32d0dbffe7f974 to your computer and use it in GitHub Desktop.
Save coderRohan123/cc504decf6102749aa32d0dbffe7f974 to your computer and use it in GitHub Desktop.
The code snippet takes in a 2D array of matches, where each match is represented by an array of two integers (winner and loser). It determines the winners in three categories: zero losses, one loss, and more than one loss. It

✅☑Beats 98.22% Users || [C++/Java/Python/JavaScript] || EXPLAINED🔥 - LeetCode Discuss Snippet

Preview:
import java.util.*;

class Solution {
    public int[][] findWinners(int[][] matches) {
        Set<Integer> zeroLoss = new HashSet<>();
        Set<Integer> oneLoss = new HashSet<>();
        Set<Integer> moreLoss = new HashSet<>();

        for (int[] match : matches) {
            int winner = match[0];
            int loser = match[1];

            // Add winner.
            if (!oneLoss.contains(winner) && !moreLoss.contains(winner)) {
                zeroLoss.add(winner);
            }

            // Add or move loser.
            if (zeroLoss.contains(loser)) {
                zeroLoss.remove(loser);
                oneLoss.add(loser);
            } else if (oneLoss.contains(loser)) {
                oneLoss.remove(loser);
                moreLoss.add(loser);
            } else if (moreLoss.contains(loser)) {
                continue;
            } else {
                oneLoss.add(loser);
            }
        }

        List<List<Integer>> answerList = new ArrayList<>();
        answerList.add(new ArrayList<>(zeroLoss));
        answerList.add(new ArrayList<>(oneLoss));

        int[][] answer = new int[2][];
        for (int i = 0; i < 2; i++) {
            answer[i] = new int[answerList.get(i).size()];
            for (int j = 0; j < answerList.get(i).size(); j++) {
                answer[i][j] = answerList.get(i).get(j);
            }
        }

        // Sorting winners in each category.
        Arrays.sort(answer[0]);
        Arrays.sort(answer[1]);

        return answer;
    }
}
Associated Context
Type Code Snippet ( .java )
Associated Tags Java HashSet Find Winners Matching Winner Add or Move loser ArrayList Sorting winners in each category List of integers Search algorithm arrays findWinners method Set data structure HashSet data structure int[][] matches sort method
📝 Custom Description PLEASE UPVOTE IF IT HELPED

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
💡 Smart Description This code snippet finds the winners in a given list of matches. It uses two sets to keep track of zero and or move losers, then sorts them in each category using an array as well as sorting it in
The code snippet takes in a 2D array of matches, where each match is represented by an array of two integers (winner and loser). It determines the winners in three categories: zero losses, one loss, and more than one loss. It
🔎 Suggested Searches Java code to find winners in a category
Related Links https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/4566935/Beats-98.22-Users-oror-C%2B%2BJavaPythonJavaScript-oror-EXPLAINED
https://leetcode.com/problems/find-players-with-zero-or-one-losses/
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=hot&query=
https://www.geeksforgeeks.org/arraylist-in-java/
https://www.geeksforgeeks.org/string-class-in-java/
https://www.geeksforgeeks.org/arrays-in-java/
https://www.techiedelight.com/bubble-sort-iterative-recursive/
https://www.geeksforgeeks.org/
https://stackoverflow.com/
https://www.programiz.com/
https://www.hackerrank.com/
https://www.w3schools.com/
https://www.baeldung.com/
https://leetcode.com/
https://www.javatpoint.com/
Related People Rohan Mallick
Sensitive Information No Sensitive Information Detected
Shareable Link https://rohan016.pieces.cloud/?p=cdf349aa8c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment