File tree 2 files changed +50
-0
lines changed 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 10
10
- Day 7: [ Cousins in Binary Tree] ( https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-seven/index.ts ) :hushed :
11
11
- Day 8: [ check Straight Line] ( https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eight/index.ts ) :grin :
12
12
- 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 :
Original file line number Diff line number Diff line change
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 ) ;
You can’t perform that action at this time.
0 commit comments