Skip to content

Commit 6da8fc1

Browse files
authored
Merge pull request #1328 from 0xff-dev/1583
Add solution and test-cases for problem 1583
2 parents a561e3e + 850ba08 commit 6da8fc1

File tree

3 files changed

+96
-26
lines changed

3 files changed

+96
-26
lines changed

leetcode/1501-1600/1583.Count-Unhappy-Friends/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [1583.Count Unhappy Friends][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+
You are given a list of `preferences` for n friends, where n is always **even**.
5+
6+
For each person `i`, `preferences[i]` contains a list of friends **sorted** in the **order of preference**. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from `0` to `n-1`.
7+
8+
All the friends are divided into pairs. The pairings are given in a list `paris`, where `pairs[i] = [xi, yi]` denotes `xi` is paired with `yi` and `yi` is paired with `xi`.
9+
10+
However, this pairing may cause some of the friends to be unhappy. A friend `x` is unhappy if `x` is paired with `y` and there exists a friend `u` who is paired with `v` but:
11+
12+
- `x` prefers `u` over `y`, and
13+
- `u` prefers `x` over `v`.
14+
15+
Return the number of unhappy friends.
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
20+
Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
21+
Output: 2
22+
Explanation:
23+
Friend 1 is unhappy because:
24+
- 1 is paired with 0 but prefers 3 over 0, and
25+
- 3 prefers 1 over 2.
26+
Friend 3 is unhappy because:
27+
- 3 is paired with 2 but prefers 1 over 2, and
28+
- 1 prefers 3 over 0.
29+
Friends 0 and 2 are happy.
1330
```
1431

15-
## 题意
16-
> ...
32+
**Example 2:**
1733

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count Unhappy Friends
23-
```go
2434
```
35+
Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
36+
Output: 0
37+
Explanation: Both friends 0 and 1 are happy.
38+
```
39+
40+
**Example 3:**
2541

42+
```
43+
Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]]
44+
Output: 4
45+
```
2646

2747
## 结语
2848

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, preferences [][]int, pairs [][]int) int {
4+
likeIndex := make([]map[int]int, len(preferences))
5+
for i, p := range preferences {
6+
likeIndex[i] = make(map[int]int)
7+
for idx, like := range p {
8+
likeIndex[i][like] = idx
9+
}
10+
}
11+
12+
pair := make(map[int]int)
13+
for _, p := range pairs {
14+
pair[p[0]] = p[1]
15+
pair[p[1]] = p[0]
16+
}
17+
var ret int
18+
for _, p := range pairs {
19+
x, y := p[0], p[1]
20+
for _, u := range preferences[x] {
21+
if u == y {
22+
break
23+
}
24+
if v, ok := pair[u]; ok {
25+
if likeIndex[u][x] < likeIndex[u][v] {
26+
ret++
27+
break
28+
}
29+
}
30+
}
31+
32+
for _, u := range preferences[y] {
33+
if u == x {
34+
break
35+
}
36+
if v, ok := pair[u]; ok {
37+
if likeIndex[u][y] < likeIndex[u][v] {
38+
ret++
39+
break
40+
}
41+
}
42+
}
43+
}
44+
return ret
545
}

leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution_test.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,41 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n int
14+
preferences [][]int
15+
pairs [][]int
16+
expect int
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", 4, [][]int{
19+
{1, 2, 3}, {3, 2, 0}, {3, 1, 0}, {1, 2, 0},
20+
}, [][]int{
21+
{0, 1}, {2, 3},
22+
}, 2},
23+
{"TestCase2", 2, [][]int{
24+
{1}, {0},
25+
}, [][]int{{1, 0}}, 0},
26+
{"TestCase3", 4, [][]int{
27+
{1, 3, 2}, {2, 3, 0}, {1, 3, 0}, {0, 2, 1},
28+
}, [][]int{{1, 3}, {0, 2}}, 4},
1929
}
2030

2131
// 开始测试
2232
for i, c := range cases {
2333
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
34+
got := Solution(c.n, c.preferences, c.pairs)
2535
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
36+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
37+
c.expect, got, c.n, c.preferences, c.pairs)
2838
}
2939
})
3040
}
3141
}
3242

33-
//压力测试
43+
// 压力测试
3444
func BenchmarkSolution(b *testing.B) {
3545
}
3646

37-
//使用案列
47+
// 使用案列
3848
func ExampleSolution() {
3949
}

0 commit comments

Comments
 (0)