代码拉取完成,页面将自动刷新
<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 class="example">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 class="example">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 class="example">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>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。