-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0997-find-the-town-judge.js
36 lines (33 loc) · 1.1 KB
/
0997-find-the-town-judge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 997. Find the Town Judge
* https://leetcode.com/problems/find-the-town-judge/
* Difficulty: Easy
*
* In a town, there are n people labeled from 1 to n. There is a rumor that one of these
* people is secretly the town judge.
*
* If the town judge exists, then:
* - The town judge trusts nobody.
* - Everybody (except for the town judge) trusts the town judge.
* - There is exactly one person that satisfies properties 1 and 2.
*
* You are given an array trust where trust[i] = [ai, bi] representing that the person labeled
* ai trusts the person labeled bi.
*
* Return the label of the town judge if the town judge exists and can be identified, or return
* -1 otherwise.
*/
/**
* @param {number} n
* @param {number[][]} trust
* @return {number}
*/
var findJudge = function(n, trust) {
if (!trust.length) {
return n === 1 ? n : -1;
}
const map = new Map();
trust.forEach(([_, value]) => map.set(value, (map.get(value) || 0 ) + 1));
const [judge, count] = [...map].sort(([,a], [,b]) => b - a)[0];
return count === n - 1 && !trust.find(([key]) => key === judge) ? judge : -1;
};