From 340b61c1f7635db9ecee6709d99056a0261961b0 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 18 Sep 2024 09:12:39 +0800 Subject: [PATCH] Add solution and test-cases for problem 1947 --- .../README.md | 37 ++++++++++++------- .../Solution.go | 34 ++++++++++++++++- .../Solution_test.go | 21 +++++------ 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/README.md b/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/README.md index a34423a70..5add57059 100755 --- a/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/README.md +++ b/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/README.md @@ -1,28 +1,37 @@ # [1947.Maximum Compatibility Score Sum][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 +There is a survey that consists of `n` questions where each question's answer is either `0` (no) or e`1` (yes). + +The survey was given to `m` students numbered from `0` to `m - 1` and `m` mentors numbered from `0` to `m - 1`. The answers of the students are represented by a 2D integer array `students` where `students[i]` is an integer array that contains the answers of the `ith` student (**0-indexed**). The answers of the mentors are represented by a 2D integer array `mentors` where `mentors[j]` is an integer array that contains the answers of the `jth` mentor (**0-indexed**). + +Each student will be assigned to **one** mentor, and each mentor will have **one** student assigned to them. The **compatibility score** of a student-mentor pair is the number of answers that are the same for both the student and the mentor. + +- For example, if the student's answers were `[1, 0, 1]` and the mentor's answers were `[0, 0, 1]`, then their compatibility score is 2 because only the second and the third answers are the same. + +You are tasked with finding the optimal student-mentor pairings to **maximize** the **sum of the compatibility scores**. + +Given `students` and `mentors`, return the **maximum compatibility score sum** that can be achieved. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]] +Output: 8 +Explanation: We assign students to mentors in the following way: +- student 0 to mentor 2 with a compatibility score of 3. +- student 1 to mentor 0 with a compatibility score of 2. +- student 2 to mentor 1 with a compatibility score of 3. +The compatibility score sum is 3 + 2 + 3 = 8. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Maximum Compatibility Score Sum -```go ``` - +Input: students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[1,1],[1,1]] +Output: 0 +Explanation: The compatibility score of any student-mentor pair is 0. +``` ## 结语 diff --git a/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution.go b/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution.go index d115ccf5e..0f1f8cd1c 100644 --- a/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution.go +++ b/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution.go @@ -1,5 +1,35 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(students [][]int, mentors [][]int) int { + var ( + dfs func(int, []bool) int + match func(int, int) int + ) + match = func(i, j int) int { + ans := 0 + for k := range students[i] { + if mentors[j][k] == students[i][k] { + ans++ + } + } + return ans + } + + dfs = func(index int, used []bool) int { + cur := 0 + if index == len(students) { + return cur + } + + for i := range mentors { + if used[i] { + continue + } + used[i] = true + cur = max(cur, match(index, i)+dfs(index+1, used)) + used[i] = false + } + return cur + } + return dfs(0, make([]bool, len(students))) } diff --git a/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution_test.go b/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution_test.go index 14ff50eb4..605f8f4f1 100644 --- a/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution_test.go +++ b/leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + students, mentors [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{1, 1, 0}, {1, 0, 1}, {0, 0, 1}}, [][]int{{1, 0, 0}, {0, 0, 1}, {1, 1, 0}}, 8}, + {"TestCase2", [][]int{{0, 0}, {0, 0}, {0, 0}}, [][]int{{1, 1}, {1, 1}, {1, 1}}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.students, c.mentors) 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.students, c.mentors) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }