Skip to content

Commit 9fedac5

Browse files
committed
✨ [997] Find the Town Judge
1 parent 774c87d commit 9fedac5

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

997/gpt_solution.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} trust
4+
* @return {number}
5+
*/
6+
function findJudge(n, trust) {
7+
const inDegree = Array(n + 1).fill(0); // point in -> being trust
8+
const outDegree = Array(n + 1).fill(0); // point out -> trust someonme
9+
10+
console.log(inDegree)
11+
console.log(outDegree)
12+
13+
for (const [a, b] of trust) {
14+
outDegree[a]++;
15+
inDegree[b]++;
16+
}
17+
18+
console.log(inDegree)
19+
console.log(outDegree)
20+
21+
for (let i = 1; i <= n; i++) {
22+
if (inDegree[i] === n - 1 && outDegree[i] === 0) {
23+
return i;
24+
}
25+
}
26+
27+
return -1;
28+
}
29+
30+
function findJudge2(n, trust) {
31+
const trustCounts = Array(n + 1).fill(0);
32+
33+
for (const [a, b] of trust) {
34+
trustCounts[a]--;
35+
trustCounts[b]++;
36+
}
37+
38+
for (let i = 1; i <= n; i++) {
39+
if (trustCounts[i] === n - 1) {
40+
return i;
41+
}
42+
}
43+
44+
return -1;
45+
}
46+
47+
48+
// let x = findJudge(2, [[1, 2]]) // 2
49+
// let x = findJudge(4, [[1, 2], [1, 3], [2, 1], [2, 3], [1, 4], [4, 3], [4, 1]]) // 3
50+
// let x = findJudge(1, [])
51+
// let x = findJudge(3, [[1, 2], [2, 3]]) // -1
52+
let x = findJudge(3, [[1, 3], [2, 3], [3, 1]]) // -1
53+
// 1 2 3
54+
// 1 -> 3
55+
// 2 -> 3
56+
// 3 -> 1
57+
console.log("x")
58+
console.log(x)

997/my_solution.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} trust
4+
* @return {number}
5+
*/
6+
const findJudge = (n, trust) => {
7+
if (1 === n) return 1;
8+
9+
// being trusted map
10+
let map = new Map(), judge = -1;
11+
12+
for (let i = 0; i < trust.length; i++) {
13+
// map.set(trust[i][0], (trustMap.get(trust[i][0]) || []).concat(trust[i][1]));
14+
map.set(trust[i][1], (map.get(trust[i][1]) || []).concat(trust[i][0]));
15+
16+
if (n - 1 === map.get(trust[i][1]).length) {
17+
judge = trust[i][1];
18+
// return trust[i][1];
19+
}
20+
}
21+
22+
console.log("potential judge")
23+
console.log(judge)
24+
console.log(map)
25+
console.log(map.get(judge))
26+
27+
// const isTrustingEachOther = (key, target) => {
28+
// if (map.get(key).includes(target)) return false;
29+
// }
30+
31+
// console.log(map)
32+
// for (let [key, trusters] of map) {
33+
// console.log("key:" + key + ", trusters:" + trusters)
34+
// for (let t of trusters) {
35+
// // isTrustingEachOther(t, key);
36+
// console.log(t)
37+
// if (!map.get(t)) continue;
38+
// if (map.get(t).includes(judge)) return -1;
39+
// // if (judge - 1 === map.get(t).length) return t;
40+
// }
41+
// }
42+
43+
44+
// console.log(map)
45+
// console.log("judge:" + judge)
46+
47+
return judge;
48+
};
49+
50+
51+
let x = findJudge(2, [[1, 2]]) // 2
52+
// let x = findJudge(4, [[1, 2], [1, 3], [2, 1], [2, 3], [1, 4], [4, 3], [4, 1]]) // 3
53+
// let x = findJudge(1, [])
54+
// let x = findJudge(3, [[1, 2], [2, 3]]) // -1
55+
// let x = findJudge(3, [[1, 3], [2, 3], [3, 1]]) // -1
56+
console.log("x")
57+
console.log(x)

997/solution.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} trust
4+
* @return {number}
5+
*/
6+
const findJudge = (n, trust) => {
7+
let map = new Map(), judge = -1;
8+
9+
for (let i = 0; i < trust.length; i++) {
10+
map.set(trust[i][1], (map.get(trust[i][1]) || []).concat(trust[i][0]));
11+
12+
if (n - 1 === map.get(trust[i][1]).length) {
13+
judge = trust[i][1];
14+
}
15+
}
16+
17+
for (let [key, trusters] of map) {
18+
for (let t of trusters) {
19+
if (map.get(t) && map.get(t).includes(key)) return -1;
20+
}
21+
}
22+
23+
24+
return -1;
25+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
- [424. Longest Repeating Character Replacement](./424/)
5656
- [647. Palindromic Substrings](./647/)
5757
- [771. Jewels and Stones](./771/)
58+
- [997. Find the Town Judge](./997/)
5859
- [1791. Find Center of Star Graph](./1791/)
5960
- [1971. Find if Path Exists in Graph](./1971/)
6061

@@ -90,5 +91,5 @@ Batch create in bash
9091
TODO: Add to TOC!
9192
-->
9293
```ssh
93-
chapter=1971 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
94+
chapter=997 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
9495
```

0 commit comments

Comments
 (0)