-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_the_town_judge.go
68 lines (58 loc) · 1.68 KB
/
find_the_town_judge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
)
//Task from: https://leetcode.com/problems/find-the-town-judge/
func findJudge(N int, trust [][]int) int {
if(len(trust) == 0) {
return N
}
trustIndex := -1
maxValue := -1
trustReceived := make([]int, N)
trustGave := make([]int, N)
for i := 0; i < len(trust); i++ {
trusted := trust[i][1] - 1
trustReceived[trusted] ++
trustGave[trust[i][0] - 1]++
if(trustReceived[trusted] > maxValue) {
maxValue = trustReceived[trusted]
trustIndex = trusted
}
}
if(trustIndex == -1 || maxValue != N - 1) {
return -1
}
if(trustGave[trustIndex] > 0) {
return -1
}
return trustIndex + 1
}
func testFindJudge() {
fmt.Println()
fmt.Println("Find Judge Task")
N := 4
trust := [][]int{{1,3},{1,4},{2,3},{2,4},{4,3}}
result := findJudge(N, trust)
fmt.Printf("Find Judge: N: %d ; input: %v ; result: %d [%t] \n", N, trust, result, (result == 3))
N = 3
trust = [][]int{{1,2},{2,3}}
result = findJudge(N, trust)
fmt.Printf("Find Judge: N: %d ; input: %v ; result: %d [%t] \n" , N, trust, result, (result == -1))
N = 2
trust = [][]int{{1,2}}
result = findJudge(N, trust)
fmt.Printf("Find Judge: N: %d ; input: %v ; result: %d [%t] \n", N, trust, result, (result == 2))
N = 2
trust = [][]int{{1,2}}
result = findJudge(N, trust)
fmt.Printf("Find Judge: N: %d ; input: %v ; result: %d [%t] \n", N, trust, result, (result == 2))
N = 3
trust = [][]int{{1,3},{2,3},{3,1}}
result = findJudge(N, trust)
fmt.Printf("Find Judge: N: %d ; input: %v ; result: %d [%t] \n", N, trust, result, (result == -1))
N = 1
trust = [][]int{}
result = findJudge(N, trust)
fmt.Printf("Find Judge: N: %d ; input: %v ; result: %d [%t] \n", N, trust, result, (result == 1))
}