列表

详情


2225. 找出输掉零场或一场比赛的玩家

给你一个整数数组 matches 其中 matches[i] = [winneri, loseri] 表示在一场比赛中 winneri 击败了 loseri

返回一个长度为 2 的列表 answer

两个列表中的值都应该按 递增 顺序返回。

注意:

 

示例 1:

输入:matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
输出:[[1,2,10],[4,5,7,8]]
解释:
玩家 1、2 和 10 都没有输掉任何比赛。
玩家 4、5、7 和 8 每个都输掉一场比赛。
玩家 3、6 和 9 每个都输掉两场比赛。
因此,answer[0] = [1,2,10] 和 answer[1] = [4,5,7,8] 。

示例 2:

输入:matches = [[2,3],[1,3],[5,4],[6,4]]
输出:[[1,2,5,6],[]]
解释:
玩家 1、2、5 和 6 都没有输掉任何比赛。
玩家 3 和 4 每个都输掉两场比赛。
因此,answer[0] = [1,2,5,6] 和 answer[1] = [] 。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: vector<vector<int>> findWinners(vector<vector<int>>& matches) { } };

php 解法, 执行用时: 652 ms, 内存消耗: 98.4 MB, 提交时间: 2024-05-22 09:32:35

class Solution {

    /**
     * @param Integer[][] $matches
     * @return Integer[][]
     */
    function findWinners($matches) {
        $hash = [];
        foreach ($matches as $mat) {
            if (!isset($hash[$mat[0]])) $hash[$mat[0]] = 0;
            $hash[$mat[1]]++;
        }
        ksort($hash);

        $res = [[],[]];
        foreach ($hash as $k => $v) {
            if ($v == 0) {
                $res[0][] = $k;
            } else if ($v == 1) {
                $res[1][] = $k;
            }
        }
        return $res;
    }
}

rust 解法, 执行用时: 65 ms, 内存消耗: 9.7 MB, 提交时间: 2024-05-22 09:29:49

use std::collections::HashMap;

impl Solution {
    pub fn find_winners(matches: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
        let mut loss_count = HashMap::new();
        for m in matches {
            loss_count.entry(m[0]).or_insert(0);
            *loss_count.entry(m[1]).or_insert(0) += 1;
        }

        let mut ans = vec![vec![], vec![]];
        for (player, cnt) in loss_count {
            if cnt < 2 {
                ans[cnt as usize].push(player);
            }
        }

        ans[0].sort_unstable();
        ans[1].sort_unstable();
        ans
    }
}

javascript 解法, 执行用时: 348 ms, 内存消耗: 90.6 MB, 提交时间: 2024-05-22 09:29:20

/**
 * @param {number[][]} matches
 * @return {number[][]}
 */
var findWinners = function(matches) {
    const lossCount = new Map();
    for (const [winner, loser] of matches) {
        if (!lossCount.has(winner)) {
            lossCount.set(winner, 0);
        }
        lossCount.set(loser, (lossCount.get(loser) ?? 0) + 1);
    }

    const ans = [[], []];
    for (const [player, cnt] of lossCount) {
        if (cnt < 2) {
            ans[cnt].push(player);
        }
    }

    ans[0].sort((a, b) => a - b);
    ans[1].sort((a, b) => a - b);
    return ans;
};

cpp 解法, 执行用时: 403 ms, 内存消耗: 160.8 MB, 提交时间: 2024-05-22 09:28:59

class Solution {
public:
    vector<vector<int>> findWinners(vector<vector<int>>& matches) {
        unordered_map<int, int> loss_count;
        for (auto& m : matches) {
            if (!loss_count.contains(m[0])) {
                loss_count[m[0]] = 0;
            }
            loss_count[m[1]]++;
        }

        vector<vector<int>> ans(2);
        for (auto& [player, cnt] : loss_count) {
            if (cnt < 2) {
                ans[cnt].push_back(player);
            }
        }
        ranges::sort(ans[0]);
        ranges::sort(ans[1]);
        return ans;
    }
};

java 解法, 执行用时: 67 ms, 内存消耗: 90.6 MB, 提交时间: 2024-05-22 09:28:31

public class Solution {
    public List<List<Integer>> findWinners(int[][] matches) {
        Map<Integer, Integer> lossCount = new HashMap<>();
        for (int[] m : matches) {
            if (!lossCount.containsKey(m[0])) {
                lossCount.put(m[0], 0);
            }
            lossCount.merge(m[1], 1, Integer::sum);
        }

        List<List<Integer>> ans = List.of(new ArrayList<>(), new ArrayList<>());
        for (Map.Entry<Integer, Integer> e : lossCount.entrySet()) {
            int cnt = e.getValue();
            if (cnt < 2) {
                ans.get(cnt).add(e.getKey());
            }
        }

        Collections.sort(ans.get(0));
        Collections.sort(ans.get(1));
        return ans;
    }
}

python3 解法, 执行用时: 250 ms, 内存消耗: 60.2 MB, 提交时间: 2024-05-22 09:28:05

class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        players = set(x for m in matches for x in m)
        loss_count = Counter(loser for _, loser in matches)
        return [sorted(x for x in players if x not in loss_count),
                sorted(x for x, c in loss_count.items() if c == 1)]

javascript 解法, 执行用时: 384 ms, 内存消耗: 83.1 MB, 提交时间: 2022-11-29 15:58:51

/**
 * @param {number[][]} matches
 * @return {number[][]}
 */
var findWinners = function(matches) {
    let wins = new Map();
    let loses = new Map();
    let ansWin = [];
    let ansLose = [];

    for(let [win, lose] of matches) {
        wins.set(win, (wins.get(win) || 0) + 1)
        loses.set(lose, (loses.get(lose) || 0) + 1)
    }   

    for(let [key, val] of loses.entries()) {
        if(val === 1) {
            ansLose.push(key)
        }
    }

    for(let [key, val] of wins.entries()) {
        if(!loses.has(key)) {
            ansWin.push(key)
        }
    }

    ansWin.sort((a, b) => a - b);
    ansLose.sort((a, b) => a - b);

    return [ansWin, ansLose]
};

golang 解法, 执行用时: 376 ms, 内存消耗: 25.8 MB, 提交时间: 2022-11-29 15:57:51

func findWinners(matches [][]int) [][]int {
	man := map[int]struct{}{}
	cnt := map[int]int{}
	for _, p := range matches {
		man[p[0]] = struct{}{}
		man[p[1]] = struct{}{} // 记录所有出现过的人
		cnt[p[1]]++            // 统计每个人输的次数
	}
	ans := [][]int{{}, {}}
	for v := range man {
		if c := cnt[v]; c < 2 {
			ans[c] = append(ans[c], v)
		}
	}
	sort.Ints(ans[0])
	sort.Ints(ans[1])
	return ans
}

python3 解法, 执行用时: 316 ms, 内存消耗: 54.2 MB, 提交时间: 2022-11-29 15:57:27

class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        freq = Counter()
        for winner, loser in matches:
            if winner not in freq:
                freq[winner] = 0
            freq[loser] += 1
        
        ans = [[], []]
        for key, value in freq.items():
            if value < 2:
                ans[value].append(key)
        
        ans[0].sort()
        ans[1].sort()
        return ans

上一题