Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2225.html

2225. Find Players With Zero or One Losses (Medium)

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 <= 105
  • matches[i].length == 2
  • 1 <= winneri, loseri <= 105
  • winneri != loseri
  • All matches[i] are unique.

Companies:
Indeed

Related Topics:
Graph

Similar Questions:

Solution 1. Hash Set and Map

Traverse the array, store the unique winners in a set win and store a mapping from person to lose count in map lose.

Traverse lose map, for each pair person, cnt, add person to vector oneLose if cnt == 1 (i.e. this person loses exactly once), and erase this person from win.

Lastly, return the elements in win and oneLose.

  • // OJ: https://leetcode.com/problems/find-players-with-zero-or-one-losses/
    // Time: O(NlogN)
    // Space: O(N)
    class Solution {
    public:
        vector<vector<int>> findWinners(vector<vector<int>>& A) {
            set<int> win;
            map<int, int> lose; // person, count
            for (auto &m : A) {
                win.insert(m[0]);
                lose[m[1]]++;
            }
            vector<int> oneLose;
            for (auto &[p, cnt] : lose) {
                if (cnt == 1) oneLose.push_back(p);
                win.erase(p);
            }
            return {vector<int>(begin(win), end(win)), oneLose};
        }
    };
    
  • class Solution:
        def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
            cnt = Counter()
            for a, b in matches:
                if a not in cnt:
                    cnt[a] = 0
                cnt[b] += 1
            ans = [[], []]
            for u, v in cnt.items():
                if v < 2:
                    ans[v].append(u)
            ans[0].sort()
            ans[1].sort()
            return ans
    
    ############
    
    # 2225. Find Players With Zero or One Losses
    # https://leetcode.com/problems/find-players-with-zero-or-one-losses/
    
    class Solution:
        def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
            win = defaultdict(int)
            lose = defaultdict(int)
            
            for x, y in matches:
                win[x] += 1
                lose[y] += 1
            
            res = [[] for _ in range(2)]
            
            for k, v in win.items():
                if lose[k] == 0:
                    res[0].append(k)
            
            for k, v in lose.items():
                if v == 1:
                    res[1].append(k)
            
            res[0].sort()
            res[1].sort()
            
            return res
    
    
  • class Solution {
        public List<List<Integer>> findWinners(int[][] matches) {
            Map<Integer, Integer> cnt = new HashMap<>();
            for (int[] m : matches) {
                int a = m[0], b = m[1];
                cnt.putIfAbsent(a, 0);
                cnt.put(b, cnt.getOrDefault(b, 0) + 1);
            }
            List<List<Integer>> ans = new ArrayList<>();
            ans.add(new ArrayList<>());
            ans.add(new ArrayList<>());
            for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {
                int u = entry.getKey();
                int v = entry.getValue();
                if (v < 2) {
                    ans.get(v).add(u);
                }
            }
            Collections.sort(ans.get(0));
            Collections.sort(ans.get(1));
            return ans;
        }
    }
    
  • func findWinners(matches [][]int) [][]int {
    	cnt := map[int]int{}
    	for _, m := range matches {
    		a, b := m[0], m[1]
    		if _, ok := cnt[a]; !ok {
    			cnt[a] = 0
    		}
    		cnt[b]++
    	}
    	ans := make([][]int, 2)
    	for u, v := range cnt {
    		if v < 2 {
    			ans[v] = append(ans[v], u)
    		}
    	}
    	sort.Ints(ans[0])
    	sort.Ints(ans[1])
    	return ans
    }
    

Discuss

https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1908802/

All Problems

All Solutions