Skip to content

Commit

Permalink
Add solution and test-cases for problem 1947
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Sep 18, 2024
1 parent 1fbf15a commit 340b61c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
37 changes: 23 additions & 14 deletions leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

0 comments on commit 340b61c

Please sign in to comment.