Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4873946

Browse files
committed
add day-ten
1 parent 03f584f commit 4873946

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
- Day 7: [Cousins in Binary Tree](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-seven/index.ts) :hushed:
1111
- Day 8: [check Straight Line](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eight/index.ts) :grin:
1212
- Day 9: [Is Perfect Square](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-nine/index.ts) :alien:
13+
- Day 10: [Find Town Judge](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-ten/index.ts) :alien:

src/may/day-ten/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number} N
3+
* @param {number[][]} trust
4+
* @return {number}
5+
*/
6+
export const findJudge = function (N: number, trust: number[][]): number {
7+
// hash table for villager
8+
const villagerMap: Set<number> = new Set();
9+
// hash table for judge
10+
const judgeMap: Map<number, number> = new Map();
11+
12+
for (let i = 0; i < trust.length; i++) {
13+
// current trust
14+
const trusted: number = trust[i][1];
15+
// current trust counts
16+
const trust_count: number = judgeMap.get(trusted);
17+
// record village
18+
villagerMap.add(trust[i][0]);
19+
// Save judge
20+
trust_count ? judgeMap.set(trusted, trust_count + 1) : judgeMap.set(trusted, 1);
21+
}
22+
23+
for (let [key, value] of judgeMap) {
24+
if (value === N - 1 && !villagerMap.has(key)) {
25+
return key;
26+
}
27+
}
28+
29+
return !trust.length ? N : -1;
30+
};
31+
32+
const a = findJudge(3, [
33+
[1, 3],
34+
[2, 3],
35+
]);
36+
const b = findJudge(3, [
37+
[1, 3],
38+
[2, 3],
39+
[3, 1],
40+
]);
41+
const c = findJudge(4, [
42+
[1, 3],
43+
[1, 4],
44+
[2, 3],
45+
[2, 4],
46+
[4, 3],
47+
]);
48+
49+
console.log('logs', a, b, c);

0 commit comments

Comments
 (0)