返回介绍

solution / 2200-2299 / 2225.Find Players With Zero or One Losses / README_EN

发布于 2024-06-17 01:03:08 字数 6493 浏览 0 评论 0 收藏 0

2225. Find Players With Zero or One Losses

中文文档

Description

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.

Solutions

Solution 1

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
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;
  }
}
class Solution {
public:
  vector<vector<int>> findWinners(vector<vector<int>>& matches) {
    unordered_map<int, int> cnt;
    for (auto& m : matches) {
      int a = m[0], b = m[1];
      if (!cnt.count(a)) cnt[a] = 0;
      ++cnt[b];
    }
    vector<vector<int>> ans(2);
    for (auto& [u, v] : cnt) {
      if (v < 2) ans[v].push_back(u);
    }
    sort(ans[0].begin(), ans[0].end());
    sort(ans[1].begin(), ans[1].end());
    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
}
function findWinners(matches: number[][]): number[][] {
  const cnt: Map<number, number> = new Map();
  for (const [a, b] of matches) {
    cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
    cnt.set(b, (cnt.get(b) || 0) + 1);
  }
  const ans: number[][] = [[], []];
  for (let [u, v] of cnt.entries()) {
    if (v < 2) {
      ans[v].push(u);
    }
  }
  ans[0].sort((a, b) => a - b);
  ans[1].sort((a, b) => a - b);
  return ans;
}
/**
 * @param {number[][]} matches
 * @return {number[][]}
 */
var findWinners = function (matches) {
  const cnt = new Map();
  for (const [a, b] of matches) {
    cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
    cnt.set(b, (cnt.get(b) || 0) + 1);
  }
  const ans = [[], []];
  for (let [u, v] of cnt.entries()) {
    if (v < 2) {
      ans[v].push(u);
    }
  }
  ans[0].sort((a, b) => a - b);
  ans[1].sort((a, b) => a - b);
  return ans;
};

Solution 2

/**
 * @param {number[][]} matches
 * @return {number[][]}
 */
var findWinners = function (matches) {
  const onlyWins = new Set(),
    oneLose = new Set(),
    moreLosses = new Set();

  for (const [winner, loser] of matches) {
    if (!moreLosses.has(loser)) {
      if (oneLose.has(loser)) {
        oneLose.delete(loser);
        moreLosses.add(loser);
      } else {
        onlyWins.delete(loser);
        oneLose.add(loser);
      }
    }

    if (!moreLosses.has(winner) && !oneLose.has(winner)) {
      onlyWins.add(winner);
    }
  }

  return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)];
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文