Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 2284 #890

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [2284.Sender With Largest Word Count][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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]`.

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.

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.

**Note**:

- Uppercase letters come before lowercase letters in lexicographical order.
- `"Alice"` and `"alice"` are distinct.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"]
Output: "Alice"
Explanation: Alice sends a total of 2 + 3 = 5 words.
userTwo sends a total of 2 words.
userThree sends a total of 3 words.
Since Alice has the largest word count, we return "Alice".
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Sender With Largest Word Count
```go
```

Input: messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
Output: "Charlie"
Explanation: Bob sends a total of 5 words.
Charlie sends a total of 5 words.
Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie.
```

## 结语

Expand Down
19 changes: 17 additions & 2 deletions leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

func Solution(messages []string, senders []string) string {
count := make(map[string]int)
mc, ma := 0, ""
for idx, msg := range messages {
count[senders[idx]] += len(strings.Split(msg, " "))
if count[senders[idx]] > mc {
mc = count[senders[idx]]
ma = senders[idx]
continue
}
if count[senders[idx]] == mc && senders[idx] > ma {
ma = senders[idx]
}
}
return ma
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
messages []string
senders []string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"}, []string{"Alice", "userTwo", "userThree", "Alice"}, "Alice"},
{"TestCase2", []string{"How is leetcode for everyone", "Leetcode is useful for practice"}, []string{"Bob", "Charlie"}, "Charlie"},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.messages, c.senders)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.messages, c.senders)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading