Skip to content

Commit de11ecd

Browse files
committed
Find the Town Judge / 湲곗�
1 parent cf8759b commit de11ecd

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var findJudge = function (n, trust) {
2+
if (n === 1) return 1;
3+
4+
const trustScore = new Array(n + 1).fill(0);
5+
6+
for (const [a, b] of trust) {
7+
trustScore[a]--;
8+
trustScore[b]++;
9+
}
10+
11+
for (let i = 1; i <= n; i++) {
12+
if (trustScore[i] === n - 1) return i;
13+
}
14+
15+
return -1;
16+
};

0 commit comments

Comments
 (0)