Find Circular Gift Exchange Chains Problem
Description
LeetCode Problem 3401.
Table: SecretSanta
+-------------+------+
| Column Name | Type |
+-------------+------+
| giver_id | int |
| receiver_id | int |
| gift_value | int |
+-------------+------+
(giver_id, receiver_id) is the unique key for this table.
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.
Write a solution to find the total gift value and length of circular chains of Secret Santa gift exchanges:
A circular chain is defined as a series of exchanges where:
- Each employee gives a gift to exactly one other employee.
- Each employee receives a gift from exactly one other employee.
- The exchanges form a continuous loop (e.g., employee A gives a gift to B, B gives to C, and C gives back to A).
Return the result ordered by the chain length and total gift value of the chain in descending order. If multiple different chains have the same chain_length and total_gift_value, include that combination only once.
The result format is in the following example.
Example:
Input:
SecretSanta table:
+----------+-------------+------------+
| giver_id | receiver_id | gift_value |
+----------+-------------+------------+
| 1 | 2 | 20 |
| 2 | 3 | 30 |
| 3 | 1 | 40 |
| 4 | 5 | 25 |
| 5 | 4 | 35 |
+----------+-------------+------------+
Output:
+----------+--------------+------------------+
| chain_id | chain_length | total_gift_value |
+----------+--------------+------------------+
| 1 | 3 | 90 |
| 2 | 2 | 60 |
+----------+--------------+------------------+
Explanation:
- Chain 1 involves employees 1, 2, and 3:
- Employee 1 gives a gift to 2, employee 2 gives a gift to 3, and employee 3 gives a gift to 1.
- Total gift value for this chain = 20 + 30 + 40 = 90.
- Chain 2 involves employees 4 and 5:
- Employee 4 gives a gift to 5, and employee 5 gives a gift to 4.
- Total gift value for this chain = 25 + 35 = 60.
The result table is ordered by the chain length and total gift value of the chain in descending order.
Solution
Approach: Keep one-in-one-out employees and walk the cycles
A circular chain is a connected group in which every employee gives exactly one gift and receives exactly one. If anyone in a group gives twice, receives twice, or only gives or only receives, the group is not a chain.
- Eligible employees. Count the gifts each employee gives and receives and keep those with exactly one of each.
- Eligible gifts. Keep only the gifts whose giver and receiver are both eligible. A cycle made of eligible people has no other gift touching it, so a cycle in this smaller graph is a whole chain. Every eligible giver has one outgoing gift, so walking forward from a person is deterministic.
- Walk. A recursive CTE starts from every eligible gift and follows the receiver's own gift, adding 1 to the length and the gift value to the total, until the walk comes back to the person it started from. Because each person receives only once, a walk can never join a cycle that it did not start on, so it ends. Chains with a gift to oneself (length 1) are dropped.
- Collect. A cycle is found once from each of its members, with the same length and total each time, so
DISTINCT (chain_length, total_gift_value)removes the repeats (the statement also asks for equal combinations to appear once).ROW_NUMBER()over the required order giveschain_id.
The Pandas version builds the eligible graph the same way and follows each cycle with a loop.
WITH RECURSIVE degree AS (
SELECT g.giver_id AS employee_id
FROM (SELECT giver_id, COUNT(*) AS gives FROM SecretSanta GROUP BY giver_id) AS g
JOIN (SELECT receiver_id, COUNT(*) AS gets FROM SecretSanta GROUP BY receiver_id) AS r
ON r.receiver_id = g.giver_id
WHERE g.gives = 1 AND r.gets = 1
),
edges AS (
-- keep only gifts between employees who give once and receive once
SELECT s.giver_id, s.receiver_id, s.gift_value
FROM SecretSanta AS s
JOIN degree AS d1 ON d1.employee_id = s.giver_id
JOIN degree AS d2 ON d2.employee_id = s.receiver_id
),
walk AS (
-- follow the gifts from every employee until we return to the start
SELECT giver_id AS start_id, receiver_id AS current_id, 1 AS chain_length, gift_value AS total_gift_value
FROM edges
UNION ALL
SELECT w.start_id, e.receiver_id, w.chain_length + 1, w.total_gift_value + e.gift_value
FROM walk AS w
JOIN edges AS e ON e.giver_id = w.current_id
WHERE w.current_id <> w.start_id
),
chains AS (
SELECT DISTINCT chain_length, total_gift_value
FROM walk
WHERE current_id = start_id AND chain_length > 1
)
SELECT
ROW_NUMBER() OVER (ORDER BY chain_length DESC, total_gift_value DESC) AS chain_id,
chain_length,
total_gift_value
FROM chains
ORDER BY chain_length DESC, total_gift_value DESC;