diff --git a/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/README.md b/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/README.md index 65ceda893..fafd0614a 100755 --- a/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/README.md +++ b/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution.go b/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution.go index d115ccf5e..eeeb16329 100644 --- a/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution.go +++ b/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution.go @@ -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 } diff --git a/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution_test.go b/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution_test.go index 14ff50eb4..82398ad20 100644 --- a/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution_test.go +++ b/leetcode/2201-2300/2284.Sender-With-Largest-Word-Count/Solution_test.go @@ -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() { }