|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 困难 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3401.Find%20Circular%20Gift%20Exchange%20Chains/README.md |
| 5 | +tags: |
| 6 | + - 数据库 |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3401. Find Circular Gift Exchange Chains 🔒](https://leetcode.cn/problems/find-circular-gift-exchange-chains) |
| 12 | + |
| 13 | +[English Version](/solution/3400-3499/3401.Find%20Circular%20Gift%20Exchange%20Chains/README_EN.md) |
| 14 | + |
| 15 | +## 题目描述 |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code>SecretSanta</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-------------+------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-------------+------+ |
| 25 | +| giver_id | int | |
| 26 | +| receiver_id | int | |
| 27 | +| gift_value | int | |
| 28 | ++-------------+------+ |
| 29 | +(giver_id, receiver_id) is the unique key for this table. |
| 30 | +Each row represents a record of a gift exchange between two employees, giver_id represents the employee who gives a gift, receiver_id represents the employee who receives the gift and gift_value represents the value of the gift given. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p>Write a solution to find the <strong>total gift value</strong> and <strong>length</strong> of<strong> circular chains</strong> of Secret Santa gift exchanges:</p> |
| 34 | + |
| 35 | +<p>A <strong>circular chain</strong> is defined as a series of exchanges where:</p> |
| 36 | + |
| 37 | +<ul> |
| 38 | + <li>Each employee gives a gift to <strong>exactly one</strong> other employee.</li> |
| 39 | + <li>Each employee receives a gift <strong>from exactly</strong> one other employee.</li> |
| 40 | + <li>The exchanges form a continuous <strong>loop</strong> (e.g., employee A gives a gift to B, B gives to C, and C gives back to A).</li> |
| 41 | +</ul> |
| 42 | + |
| 43 | +<p>Return <em>the result ordered by the chain length and total gift value of the chain in <strong>descending</strong> order</em>. </p> |
| 44 | + |
| 45 | +<p>The result format is in the following example.</p> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | +<p><strong class="example">Example:</strong></p> |
| 49 | + |
| 50 | +<div class="example-block"> |
| 51 | +<p><strong>Input:</strong></p> |
| 52 | + |
| 53 | +<p>SecretSanta table:</p> |
| 54 | + |
| 55 | +<pre class="example-io"> |
| 56 | ++----------+-------------+------------+ |
| 57 | +| giver_id | receiver_id | gift_value | |
| 58 | ++----------+-------------+------------+ |
| 59 | +| 1 | 2 | 20 | |
| 60 | +| 2 | 3 | 30 | |
| 61 | +| 3 | 1 | 40 | |
| 62 | +| 4 | 5 | 25 | |
| 63 | +| 5 | 4 | 35 | |
| 64 | ++----------+-------------+------------+ |
| 65 | +</pre> |
| 66 | + |
| 67 | +<p><strong>Output:</strong></p> |
| 68 | + |
| 69 | +<pre class="example-io"> |
| 70 | ++----------+--------------+------------------+ |
| 71 | +| chain_id | chain_length | total_gift_value | |
| 72 | ++----------+--------------+------------------+ |
| 73 | +| 1 | 3 | 90 | |
| 74 | +| 2 | 2 | 60 | |
| 75 | ++----------+--------------+------------------+ |
| 76 | +</pre> |
| 77 | + |
| 78 | +<p><strong>Explanation:</strong></p> |
| 79 | + |
| 80 | +<ul> |
| 81 | + <li><strong>Chain 1</strong> involves employees 1, 2, and 3: |
| 82 | + |
| 83 | + <ul> |
| 84 | + <li>Employee 1 gives a gift to 2, employee 2 gives a gift to 3, and employee 3 gives a gift to 1.</li> |
| 85 | + <li>Total gift value for this chain = 20 + 30 + 40 = 90.</li> |
| 86 | + </ul> |
| 87 | + </li> |
| 88 | + <li><strong>Chain 2</strong> involves employees 4 and 5: |
| 89 | + <ul> |
| 90 | + <li>Employee 4 gives a gift to 5, and employee 5 gives a gift to 4.</li> |
| 91 | + <li>Total gift value for this chain = 25 + 35 = 60.</li> |
| 92 | + </ul> |
| 93 | + </li> |
| 94 | + |
| 95 | +</ul> |
| 96 | + |
| 97 | +<p>The result table is ordered by the chain length and total gift value of the chain in descending order.</p> |
| 98 | +</div> |
| 99 | + |
| 100 | +<!-- description:end --> |
| 101 | + |
| 102 | +## 解法 |
| 103 | + |
| 104 | +<!-- solution:start --> |
| 105 | + |
| 106 | +### 方法一 |
| 107 | + |
| 108 | +<!-- tabs:start --> |
| 109 | + |
| 110 | +#### MySQL |
| 111 | + |
| 112 | +```sql |
| 113 | + |
| 114 | +``` |
| 115 | + |
| 116 | +<!-- tabs:end --> |
| 117 | + |
| 118 | +<!-- solution:end --> |
| 119 | + |
| 120 | +<!-- problem:end --> |
0 commit comments