Election Results Problem
Description
LeetCode Problem 2820.
Table: Votes
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| voter | varchar |
| candidate | varchar |
+-------------+---------+
(voter, candidate) is the primary key (combination of unique values) for this table.
Each row of this table contains name of the voter and their candidate.
The election is conducted in a city where everyone can vote for one or more candidates or choose not to vote. Each person has 1 vote so if they vote for multiple candidates, their vote gets equally split across them. For example, if a person votes for 2 candidates, these candidates receive an equivalent of 0.5 votes each.
Write a solution to find candidate who got the most votes and won the election. Output the name of the candidate or If multiple candidates have an equal number of votes, display the names of all of them.
Return the result table ordered by candidate in ascending order.
The result format is in the following example.
Example 1:
Input:
Votes table:
+----------+-----------+
| voter | candidate |
+----------+-----------+
| Kathy | null |
| Charles | Ryan |
| Charles | Christine |
| Charles | Kathy |
| Benjamin | Christine |
| Anthony | Ryan |
| Edward | Ryan |
| Terry | null |
| Evelyn | Kathy |
| Arthur | Christine |
+----------+-----------+
Output:
+-----------+
| candidate |
+-----------+
| Christine |
| Ryan |
+-----------+
Explanation:
- Kathy and Terry opted not to participate in voting, resulting in their votes being recorded as 0. Charles distributed his vote among three candidates, equating to 0.33 for each candidate. On the other hand, Benjamin, Arthur, Anthony, Edward, and Evelyn each cast their votes for a single candidate.
- Collectively, Candidate Ryan and Christine amassed a total of 2.33 votes, while Kathy received a combined total of 1.33 votes.
Since Ryan and Christine received an equal number of votes, we will display their names in ascending order.
Solution
Approach: Split each vote, sum per candidate, keep the top
A voter who picks k candidates gives each of them 1/k, and voters who picked nobody (candidate IS NULL) give nothing.
- Drop the
NULLrows first, then compute each row's share as1 / COUNT(*) OVER (PARTITION BY voter). - Sum the shares per candidate.
- Rank the totals descending and keep rank 1.
RANKreturns every candidate tied for first, which the problem asks for. Sort by name.
Do the NULL filter before counting: otherwise a NULL row would count as a candidate in the voter's split.
In Pandas the same steps are dropna, a transform('count') per voter, a groupby sum and a comparison with the maximum (rounded, to avoid floating-point noise in ties).
WITH shares AS (
SELECT
candidate,
1 / COUNT(*) OVER (PARTITION BY voter) AS vote
FROM Votes
WHERE candidate IS NOT NULL
),
totals AS (
SELECT candidate, SUM(vote) AS total
FROM shares
GROUP BY candidate
),
ranked AS (
SELECT candidate, RANK() OVER (ORDER BY total DESC) AS rk
FROM totals
)
SELECT candidate
FROM ranked
WHERE rk = 1
ORDER BY candidate;