-
Notifications
You must be signed in to change notification settings - Fork 0
/
2284. Sender With Largest Word Count.go
74 lines (68 loc) · 1.32 KB
/
2284. Sender With Largest Word Count.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
package contest
import (
"sort"
"strings"
)
//Optimized solution
func largestWordCount(messages []string, senders []string) string {
msgCount := make(map[string]int)
for i, sender := range senders {
count := 1
for _, ch := range messages[i] {
if ch == ' ' {
count++
}
}
msgCount[sender] += count
}
var (
max = 0
ans string
)
for sender, words := range msgCount {
switch {
case max > words:
case max < words:
max = words
fallthrough
case ans < sender:
ans = sender
}
}
return ans
}
//First Submission
func largestWordCount2(messages []string, senders []string) string {
m := make(map[string]int)
for i, sender := range senders {
sp := strings.Split(messages[i], " ")
m[sender] += len(sp)
}
mapConv := converter(m)
sort.Slice(mapConv, func(i, j int) bool {
return mapConv[i].wordCount > mapConv[j].wordCount
})
name := mapConv[0].name
for i := 0; i < len(mapConv)-1; i++ {
if mapConv[i].wordCount == mapConv[i+1].wordCount {
a := strings.Compare(name, mapConv[i+1].name)
if a < 0 {
name = mapConv[i+1].name
}
} else {
break
}
}
return name
}
type Msg struct {
name string
wordCount int
}
func converter(m map[string]int) []Msg {
res := make([]Msg, 0, len(m))
for i, val := range m {
res = append(res, Msg{name: i, wordCount: val})
}
return res
}