mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
64 lines
2.9 KiB
HTML
64 lines
2.9 KiB
HTML
<p>You are given a list of <code>preferences</code> for <code>n</code> friends, where <code>n</code> is always <strong>even</strong>.</p>
|
|
|
|
<p>For each person <code>i</code>, <code>preferences[i]</code> contains a list of friends <strong>sorted</strong> in the <strong>order of preference</strong>. 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 <code>0</code> to <code>n-1</code>.</p>
|
|
|
|
<p>All the friends are divided into pairs. The pairings are given in a list <code>pairs</code>, where <code>pairs[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> denotes <code>x<sub>i</sub></code> is paired with <code>y<sub>i</sub></code> and <code>y<sub>i</sub></code> is paired with <code>x<sub>i</sub></code>.</p>
|
|
|
|
<p>However, this pairing may cause some of the friends to be unhappy. A friend <code>x</code> is unhappy if <code>x</code> is paired with <code>y</code> and there exists a friend <code>u</code> who is paired with <code>v</code> but:</p>
|
|
|
|
<ul>
|
|
<li><code>x</code> prefers <code>u</code> over <code>y</code>, and</li>
|
|
<li><code>u</code> prefers <code>x</code> over <code>v</code>.</li>
|
|
</ul>
|
|
|
|
<p>Return <em>the number of unhappy friends</em>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
|
|
<strong>Output:</strong> 2
|
|
<strong>Explanation:</strong>
|
|
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.
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
|
|
<strong>Output:</strong> 0
|
|
<strong>Explanation:</strong> Both friends 0 and 1 are happy.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]]
|
|
<strong>Output:</strong> 4
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>2 <= n <= 500</code></li>
|
|
<li><code>n</code> is even.</li>
|
|
<li><code>preferences.length == n</code></li>
|
|
<li><code>preferences[i].length == n - 1</code></li>
|
|
<li><code>0 <= preferences[i][j] <= n - 1</code></li>
|
|
<li><code>preferences[i]</code> does not contain <code>i</code>.</li>
|
|
<li>All values in <code>preferences[i]</code> are unique.</li>
|
|
<li><code>pairs.length == n/2</code></li>
|
|
<li><code>pairs[i].length == 2</code></li>
|
|
<li><code>x<sub>i</sub> != y<sub>i</sub></code></li>
|
|
<li><code>0 <= x<sub>i</sub>, y<sub>i</sub> <= n - 1</code></li>
|
|
<li>Each person is contained in <strong>exactly one</strong> pair.</li>
|
|
</ul>
|