diff --git a/leetcode/1501-1600/1583.Count-Unhappy-Friends/README.md b/leetcode/1501-1600/1583.Count-Unhappy-Friends/README.md index 49fdd8431..9f6706d24 100755 --- a/leetcode/1501-1600/1583.Count-Unhappy-Friends/README.md +++ b/leetcode/1501-1600/1583.Count-Unhappy-Friends/README.md @@ -1,28 +1,48 @@ # [1583.Count Unhappy Friends][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 list of `preferences` for n friends, where n is always **even**. + +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`. + +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`. + +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: + +- `x` prefers `u` over `y`, and +- `u` prefers `x` over `v`. + +Return the number of unhappy friends. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] +Output: 2 +Explanation: +Friend 1 is unhappy because: +- 1 is paired with 0 but prefers 3 over 0, and +- 3 prefers 1 over 2. +Friend 3 is unhappy because: +- 3 is paired with 2 but prefers 1 over 2, and +- 1 prefers 3 over 0. +Friends 0 and 2 are happy. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Count Unhappy Friends -```go ``` +Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] +Output: 0 +Explanation: Both friends 0 and 1 are happy. +``` + +**Example 3:** +``` +Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] +Output: 4 +``` ## 结语 diff --git a/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution.go b/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution.go index d115ccf5e..79a151d70 100644 --- a/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution.go +++ b/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution.go @@ -1,5 +1,45 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, preferences [][]int, pairs [][]int) int { + likeIndex := make([]map[int]int, len(preferences)) + for i, p := range preferences { + likeIndex[i] = make(map[int]int) + for idx, like := range p { + likeIndex[i][like] = idx + } + } + + pair := make(map[int]int) + for _, p := range pairs { + pair[p[0]] = p[1] + pair[p[1]] = p[0] + } + var ret int + for _, p := range pairs { + x, y := p[0], p[1] + for _, u := range preferences[x] { + if u == y { + break + } + if v, ok := pair[u]; ok { + if likeIndex[u][x] < likeIndex[u][v] { + ret++ + break + } + } + } + + for _, u := range preferences[y] { + if u == x { + break + } + if v, ok := pair[u]; ok { + if likeIndex[u][y] < likeIndex[u][v] { + ret++ + break + } + } + } + } + return ret } diff --git a/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution_test.go b/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution_test.go index 14ff50eb4..1ed9e628d 100644 --- a/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution_test.go +++ b/leetcode/1501-1600/1583.Count-Unhappy-Friends/Solution_test.go @@ -9,31 +9,41 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + n int + preferences [][]int + pairs [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 4, [][]int{ + {1, 2, 3}, {3, 2, 0}, {3, 1, 0}, {1, 2, 0}, + }, [][]int{ + {0, 1}, {2, 3}, + }, 2}, + {"TestCase2", 2, [][]int{ + {1}, {0}, + }, [][]int{{1, 0}}, 0}, + {"TestCase3", 4, [][]int{ + {1, 3, 2}, {2, 3, 0}, {1, 3, 0}, {0, 2, 1}, + }, [][]int{{1, 3}, {0, 2}}, 4}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.preferences, c.pairs) 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 %v", + c.expect, got, c.n, c.preferences, c.pairs) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }