From ba1219bf22cc682945d0cbb05cd2dfba01f2e339 Mon Sep 17 00:00:00 2001 From: Yuya Minamide Date: Sun, 25 Jun 2023 18:06:59 -0700 Subject: [PATCH] solved 997. Find the Town Judge --- 997.find-the-town-judge.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 997.find-the-town-judge.js diff --git a/997.find-the-town-judge.js b/997.find-the-town-judge.js new file mode 100644 index 0000000..6dd327b --- /dev/null +++ b/997.find-the-town-judge.js @@ -0,0 +1,29 @@ +/** + * URL of this problem + * https://leetcode.com/problems/find-the-town-judge/ + */ + +/** + * @param {number} n + * @param {number[][]} trust + * @return {number} + */ +var findJudge = function (n, trust) { + if (!trust.length && n <= 1) return n; + + const map1 = {}; + const map2 = {}; + + for (let i = 0; i < trust.length; i++) { + map1[trust[i][0]] = map1[trust[i][0]] + 1 || 1; + map2[trust[i][1]] = map2[trust[i][1]] + 1 || 1; + } + + for (let i = 1; i <= n; i++) { + if (map2[i] === n - 1 && map1[i] === undefined) { + return i; + } + } + + return -1; +};