Skip to content

Commit ffaca5d

Browse files
authored
Merge pull request #987 from 0xff-dev/1947
Add solution and test-cases for problem 1947
2 parents 5e475da + 340b61c commit ffaca5d

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [1947.Maximum Compatibility Score Sum][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is a survey that consists of `n` questions where each question's answer is either `0` (no) or e`1` (yes).
5+
6+
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**).
7+
8+
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.
9+
10+
- 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.
11+
12+
You are tasked with finding the optimal student-mentor pairings to **maximize** the **sum of the compatibility scores**.
13+
14+
Given `students` and `mentors`, return the **maximum compatibility score sum** that can be achieved.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]]
20+
Output: 8
21+
Explanation: We assign students to mentors in the following way:
22+
- student 0 to mentor 2 with a compatibility score of 3.
23+
- student 1 to mentor 0 with a compatibility score of 2.
24+
- student 2 to mentor 1 with a compatibility score of 3.
25+
The compatibility score sum is 3 + 2 + 3 = 8.
1326
```
1427

15-
## 题意
16-
> ...
17-
18-
## 题解
28+
**Example 2:**
1929

20-
### 思路1
21-
> ...
22-
Maximum Compatibility Score Sum
23-
```go
2430
```
25-
31+
Input: students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[1,1],[1,1]]
32+
Output: 0
33+
Explanation: The compatibility score of any student-mentor pair is 0.
34+
```
2635

2736
## 结语
2837

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(students [][]int, mentors [][]int) int {
4+
var (
5+
dfs func(int, []bool) int
6+
match func(int, int) int
7+
)
8+
match = func(i, j int) int {
9+
ans := 0
10+
for k := range students[i] {
11+
if mentors[j][k] == students[i][k] {
12+
ans++
13+
}
14+
}
15+
return ans
16+
}
17+
18+
dfs = func(index int, used []bool) int {
19+
cur := 0
20+
if index == len(students) {
21+
return cur
22+
}
23+
24+
for i := range mentors {
25+
if used[i] {
26+
continue
27+
}
28+
used[i] = true
29+
cur = max(cur, match(index, i)+dfs(index+1, used))
30+
used[i] = false
31+
}
32+
return cur
33+
}
34+
return dfs(0, make([]bool, len(students)))
535
}

leetcode/1901-2000/1947.Maximum-Compatibility-Score-Sum/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
students, mentors [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 1, 0}, {1, 0, 1}, {0, 0, 1}}, [][]int{{1, 0, 0}, {0, 0, 1}, {1, 1, 0}}, 8},
17+
{"TestCase2", [][]int{{0, 0}, {0, 0}, {0, 0}}, [][]int{{1, 1}, {1, 1}, {1, 1}}, 0},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.students, c.mentors)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.students, c.mentors)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)