Skip to content

Commit 31150d9

Browse files
authored
Merge pull request #890 from 0xff-dev/2284
Add solution and test-cases for problem 2284
2 parents 433ae70 + 786550c commit 31150d9

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [2284.Sender With Largest Word Count][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You have a chat log of `n` messages. You are given two string arrays `messages` and `senders` where `messages[i]` is a **message** sent by `senders[i]`.
5+
6+
A *message** is list of **words** that are separated by a single space with no leading or trailing spaces. The **word count** of a sender is the total number of **words** sent by the sender. Note that a sender may send more than one message.
7+
8+
Return the sender with the **largest** word count. If there is more than one sender with the largest word count, return the one with the **lexicographically largest** name.
9+
10+
**Note**:
11+
12+
- Uppercase letters come before lowercase letters in lexicographical order.
13+
- `"Alice"` and `"alice"` are distinct.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"]
19+
Output: "Alice"
20+
Explanation: Alice sends a total of 2 + 3 = 5 words.
21+
userTwo sends a total of 2 words.
22+
userThree sends a total of 3 words.
23+
Since Alice has the largest word count, we return "Alice".
1324
```
1425

15-
## 题意
16-
> ...
17-
18-
## 题解
26+
**Example 2:**
1927

20-
### 思路1
21-
> ...
22-
Sender With Largest Word Count
23-
```go
2428
```
25-
29+
Input: messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
30+
Output: "Charlie"
31+
Explanation: Bob sends a total of 5 words.
32+
Charlie sends a total of 5 words.
33+
Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie.
34+
```
2635

2736
## 结语
2837

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(messages []string, senders []string) string {
6+
count := make(map[string]int)
7+
mc, ma := 0, ""
8+
for idx, msg := range messages {
9+
count[senders[idx]] += len(strings.Split(msg, " "))
10+
if count[senders[idx]] > mc {
11+
mc = count[senders[idx]]
12+
ma = senders[idx]
13+
continue
14+
}
15+
if count[senders[idx]] == mc && senders[idx] > ma {
16+
ma = senders[idx]
17+
}
18+
}
19+
return ma
520
}

leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
messages []string
14+
senders []string
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"}, []string{"Alice", "userTwo", "userThree", "Alice"}, "Alice"},
18+
{"TestCase2", []string{"How is leetcode for everyone", "Leetcode is useful for practice"}, []string{"Bob", "Charlie"}, "Charlie"},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.messages, c.senders)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.messages, c.senders)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)