Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

997. 找到小镇的法官 #32

Open
zpc7 opened this issue Aug 13, 2022 · 0 comments
Open

997. 找到小镇的法官 #32

zpc7 opened this issue Aug 13, 2022 · 0 comments
Labels
简单 LeetCode 难度定级

Comments

@zpc7
Copy link
Owner

zpc7 commented Aug 13, 2022

997. 找到小镇的法官

function findJudge(n: number, trust: number[][]): number {
    // 寻找图中出度为 0 的节点

    // key:被信任的人,value:被多少人信任
    let peopleWhoBeTrustedMap = new Map();
    // 信任别人的人
    let peopleWhoTrustOther = new Set();
    trust.forEach(item => {
        peopleWhoBeTrustedMap.set(item[1], (peopleWhoBeTrustedMap.get(item[1]) || 0) + 1)
        peopleWhoTrustOther.add(item[0])
    })

    let res = -1; //法官

    for (let i = 1; i <= n; i++) {
        // !peopleWhoTrustOther.has(i) 法官不信任其他人
        // (peopleWhoBeTrustedMap.get(i) || 0) === n - 1 其他人都信任法官
        if (!peopleWhoTrustOther.has(i) && (peopleWhoBeTrustedMap.get(i) || 0) === n - 1) {
            res = i;
        }
    }

    return res;
};
@zpc7 zpc7 added 简单 LeetCode 难度定级 JS and removed JS labels Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
简单 LeetCode 难度定级
Projects
None yet
Development

No branches or pull requests

1 participant