diff --git a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md index fbf3d095..eac7e475 100755 --- a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md +++ b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/README.md @@ -1,28 +1,37 @@ # [3527.Find the Most Common Response][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 are given a 2D string array `responses` where each `responses[i]` is an array of strings representing survey responses from the `ith` day. + +Return the **most common** response across all days after removing **duplicate** responses within each `responses[i]`. If there is a tie, return the lexicographically smallest response. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: responses = [["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]] -## 题意 -> ... +Output: "good" -## 题解 +Explanation: -### 思路1 -> ... -Find the Most Common Response -```go +After removing duplicates within each list, responses = [["good", "ok"], ["ok", "bad", "good"], ["good"], ["bad"]]. +"good" appears 3 times, "ok" appears 2 times, and "bad" appears 2 times. +Return "good" because it has the highest frequency. ``` +**Example 2:** + +``` +Input: responses = [["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]] + +Output: "bad" + +Explanation: + +After removing duplicates within each list we have responses = [["good", "ok"], ["ok", "bad"], ["bad", "notsure"], ["great", "good"]]. +"bad", "good", and "ok" each occur 2 times. +The output is "bad" because it is the lexicographically smallest amongst the words with the highest frequency. +``` ## 结语 diff --git a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go index d115ccf5..82f681cd 100644 --- a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go +++ b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution.go @@ -1,5 +1,31 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(responses [][]string) string { + store := make(map[string]int) + for _, resp := range responses { + cur := make(map[string]struct{}) + for _, str := range resp { + cur[str] = struct{}{} + } + for key := range cur { + store[key]++ + } + } + var ( + ret string + cnt int + ) + for key, count := range store { + if count > cnt { + ret = key + cnt = count + continue + } + + if count == cnt { + ret = min(ret, key) + } + } + + return ret } diff --git a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go index 14ff50eb..039e61a8 100644 --- a/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go +++ b/leetcode/3501-3600/3527.Find-the-Most-Common-Response/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]string + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]string{{"good", "ok", "good", "ok"}, {"ok", "bad", "good", "ok", "ok"}, {"good"}, {"bad"}}, "good"}, + {"TestCase2", [][]string{{"good", "ok", "good"}, {"ok", "bad"}, {"bad", "notsure"}, {"great", "good"}}, "bad"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }