From 37e13b988773201c0c94c7ca1bb6b95b76ab1f2c Mon Sep 17 00:00:00 2001 From: Shahriar Shatil <52494840+ShatilKhan@users.noreply.github.com> Date: Thu, 22 Feb 2024 23:39:54 +0600 Subject: [PATCH] Time: 85 ms (71.00%) | Memory: 58.1 MB (22.50%) - LeetSync --- .../find-the-town-judge.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1039-find-the-town-judge/find-the-town-judge.js diff --git a/1039-find-the-town-judge/find-the-town-judge.js b/1039-find-the-town-judge/find-the-town-judge.js new file mode 100644 index 0000000..795761c --- /dev/null +++ b/1039-find-the-town-judge/find-the-town-judge.js @@ -0,0 +1,19 @@ +/** + * @param {number} n + * @param {number[][]} trust + * @return {number} + */ +var findJudge = function(N, trust) { + const inDegree = new Array(N + 1).fill(0); + const outDegree = new Array(N + 1).fill(0); + for (let a of trust) { + outDegree[a[0]]++; + inDegree[a[1]]++; + } + for (let i = 1; i <= N; ++i) { + if (inDegree[i] === N - 1 && outDegree[i] === 0) + return i; + } + return -1; +}; +