1. 程式人生 > 其它 >[LeetCode] 2225. Find Players With Zero or One Losses

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

找出輸掉零場或一場比賽的玩家。

給你一個整數陣列 matches 其中 matches[i] = [winneri, loseri] 表示在一場比賽中 winneri 擊敗了 loseri 。

返回一個長度為 2 的列表 answer :

answer[0] 是所有 沒有 輸掉任何比賽的玩家列表。
answer[1] 是所有恰好輸掉 一場 比賽的玩家列表。
兩個列表中的值都應該按 遞增 順序返回。

注意:

只考慮那些參與 至少一場 比賽的玩家。
生成的測試用例保證 不存在 兩場比賽結果 相同 。

來源:力扣(LeetCode)
連結:https://leetcode.cn/problems/find-players-with-zero-or-one-losses
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這道題不難,細心就行。思路是用雜湊表儲存所有出現過的玩家分別輸了幾場比賽,贏的場次不需要記錄。注意返回之前需要把兩個 sublist 分別排序。

時間O(nlogn)

空間O(n)

Java實現

 1 class Solution {
 2     public List<List<Integer>> findWinners(int[][] matches) {
 3         HashMap<Integer, int[]> map = new HashMap<>();
 4         for (int[] m : matches) {
 5             int winner = m[0];
 6             int loser = m[1];
 7             if (!map.containsKey(winner)) {
 8                 map.put(winner, new int[2]);
 9             }
10             if (!map.containsKey(loser)) {
11                 map.put(loser, new int[2]);
12             }
13             map.get(winner)[0]++;
14             map.get(loser)[1]++;
15         }
16 
17         List<List<Integer>> res = new ArrayList<>();
18         List<Integer> list1 = new ArrayList<>();
19         List<Integer> list2 = new ArrayList<>();
20         for (int key : map.keySet()) {
21             if (map.get(key)[1] == 0) {
22                 list1.add(key);
23             }
24             if (map.get(key)[1] == 1) {
25                 list2.add(key);
26             }
27         }
28         Collections.sort(list1);
29         Collections.sort(list2);
30         res.add(list1);
31         res.add(list2);
32         return res;
33     }
34 }

LeetCode 題目總結