-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
220 lines (206 loc) · 3.23 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"log"
"sync"
)
type flight struct {
Start string
End string
}
var oneFlight = []flight{
{
Start: "ATL",
End: "EWR",
},
}
// start: BGY; end: AKL
var flights = []flight{
{
Start: "BCN",
End: "PSC",
},
{
Start: "JFK",
End: "AAL",
},
{
Start: "FCO",
End: "BCN",
},
{
Start: "GSO",
End: "IND",
},
{
Start: "SFO",
End: "ATL",
},
{
Start: "AAL",
End: "HEL",
},
{
Start: "PSC",
End: "BLQ",
},
{
Start: "IND",
End: "EWR",
},
{
Start: "BGY",
End: "RAR",
},
{
Start: "BJZ",
End: "AKL",
},
{
Start: "AUH",
End: "FCO",
},
{
Start: "HEL",
End: "CAK",
},
{
Start: "RAR",
End: "AUH",
},
{
Start: "CAK",
End: "BJZ",
},
{
Start: "ATL",
End: "GSO",
},
{
Start: "CHI",
End: "JFK",
},
{
Start: "BLQ",
End: "MAD",
},
{
Start: "EWR",
End: "CHI",
},
{
Start: "MAD",
End: "SFO",
},
}
func main() {
log.Println(sliceDiff(flights))
log.Println(mapDiff(flights))
log.Println(doubleFor(flights, map[string]bool{}, map[string]bool{}))
log.Println(doubleForCh(flights, &sync.Map{}, &sync.Map{}))
}
func sliceDiff(flights []flight) (string, string) {
starts := make([]string, len(flights))
ends := make([]string, len(flights))
i := 0
for _, v := range flights {
starts[i] = v.Start
ends[i] = v.End
i++
}
return sDiff(starts, ends), sDiff(ends, starts)
}
func sDiff(a, b []string) string {
bMap := make(map[string]bool, len(b))
for _, v := range b {
bMap[v] = true
}
diff := ""
for _, v := range a {
if !bMap[v] {
diff = v
break
}
}
return diff
}
func mapDiff(flights []flight) (string, string) {
starts := map[string]bool{}
ends := map[string]bool{}
for _, v := range flights {
starts[v.Start] = true
ends[v.End] = true
}
return mDiff(starts, ends), mDiff(ends, starts)
}
func mDiff(a, b map[string]bool) string {
sSlice := make([]string, len(a))
diff := ""
i := 0
for k := range a {
sSlice[i] = k
i++
}
for j := range sSlice {
if !b[sSlice[j]] {
diff = sSlice[j]
}
}
return diff
}
func doubleFor(flights []flight, s, e map[string]bool) (string, string) {
start := ""
end := ""
for _, v := range flights {
for _, w := range flights {
if start != "" || (!s[v.Start] && v.Start == w.End) {
s[v.Start] = true
}
if end != "" || (!e[v.End] && v.End == w.Start) {
e[v.End] = true
}
}
if !s[v.Start] {
start = v.Start
}
if !e[v.End] {
end = v.End
}
if start != "" && end != "" {
break
}
}
return start, end
}
func doubleForCh(flights []flight, s, e *sync.Map) (string, string) {
start := make(chan string, 1)
end := make(chan string, 1)
wg := sync.WaitGroup{}
for _, v := range flights {
wg.Add(1)
go func(v flight) {
defer wg.Done()
for _, w := range flights {
_, nSFound := s.Load(v.Start)
_, nEFound := e.Load(v.End)
if !nSFound && v.Start == w.End {
s.Store(v.Start, true)
}
if !nEFound && v.End == w.Start {
e.Store(v.End, true)
}
}
if _, ok := s.Load(v.Start); !ok {
start <- v.Start
}
if _, ok := e.Load(v.End); !ok {
end <- v.End
}
}(v)
}
go func(st, en chan string) {
wg.Wait()
close(st)
close(en)
}(start, end)
return <-start, <-end
}