-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (123 loc) · 3.46 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"errors"
"fmt"
)
func main() {
fmt.Println("Please see the unit test. (run it with cmd: go test -v -run TestMinMalwareSpread)")
}
func minMalwareSpread(graph [][]int, initial []int) map[int][]int {
goodNodeCountByRemoveBadeNode := map[int]int{}
for _, v := range initial {
goodNodeCountByRemoveBadeNode[v] = findOutGoodNodeCount(removeBadNodeConnection(graph, v), initial)
}
return findOutBigElement(goodNodeCountByRemoveBadeNode)
}
func findOutBigElement(originalMap map[int]int) map[int][]int {
bigest := 0
for _, v := range originalMap {
if v > bigest {
bigest = v
}
}
res := map[int][]int{}
for k, v := range originalMap {
if v == bigest {
res[v] = append(res[v], k)
}
}
return res
}
func findOutGoodNodeCount(graph [][]int, initial []int) int {
total := 0
connectedGoodNodeArr := []int{}
for nodeIdx, _ := range graph {
if isBadNode(nodeIdx, initial) {
continue
}
if alreadyCount(connectedGoodNodeArr, nodeIdx) {
continue
}
countByOne, connectedNodeArrByOne, err := countConnectedGoodNode(graph, nodeIdx, initial, connectedGoodNodeArr)
if err != nil {
fmt.Printf("nodeIdx : %v err: %v\n", nodeIdx, err.Error())
continue
}
if countByOne > 0 {
total = total + countByOne
connectedGoodNodeArr = append(connectedGoodNodeArr, connectedNodeArrByOne...)
}
}
return total
}
// return count and connected node array.
func countConnectedGoodNode(graph [][]int, goodNodeIdx int, badNodeArr, connectedNodeArr []int) (int, []int, error) {
count := 0
if len(graph) < goodNodeIdx {
return 0, nil, errors.New("len(graph) < goodNodeIdx")
}
for connectedNodeIdx, connectedNodeStatus := range graph[goodNodeIdx] {
if connectedNodeStatus == 1 && isBadNode(connectedNodeIdx, badNodeArr) {
return 0, nil, errors.New(fmt.Sprintf("find out connected bad node idx %v by node %v", connectedNodeIdx, goodNodeIdx))
}
if alreadyCount(connectedNodeArr, connectedNodeIdx) {
continue
}
if connectedNodeStatus == 1 {
count = count + 1
connectedNodeArr = append(connectedNodeArr, connectedNodeIdx)
nextconnectedNodeCount, nextconnectedNodeArr, err := countConnectedGoodNode(graph, connectedNodeIdx, badNodeArr, connectedNodeArr)
if err != nil {
return 0, nil, err
}
count = count + nextconnectedNodeCount
connectedNodeArr = append(connectedNodeArr, nextconnectedNodeArr...)
}
}
return count, connectedNodeArr, nil
}
func alreadyCount(nodelist []int, nodeIdx int) bool {
for _, node := range nodelist {
if nodeIdx == node {
return true
}
}
return false
}
func isBadNode(nodeIdx int, badNodeIdxArr []int) bool {
for _, badNode := range badNodeIdxArr {
if nodeIdx == badNode {
return true
}
}
return false
}
func removeBadNodeConnection(graph [][]int, badIdx int) [][]int {
res := make([][]int, 0, len(graph))
for nodeIdx, v := range graph {
//reset connected node value(0) of the bad node.
if nodeIdx == badIdx {
connectArr := make([]int, 0, len(v))
for k, _ := range v {
// except itself.
if k == badIdx {
connectArr = append(connectArr, 1)
continue
}
connectArr = append(connectArr, 0)
}
res = append(res, connectArr)
continue
}
connectArr := make([]int, 0, len(v))
for connectIdx, connectValue := range v {
if connectIdx == badIdx && connectValue == 1 {
connectArr = append(connectArr, 0)
} else {
connectArr = append(connectArr, connectValue)
}
}
res = append(res, connectArr)
}
return res
}