Practice/LinkedIn/Design an Algorithm to Elect a Restaurant for a Team Outing
CodingOptional
You're organizing a team outing and need to select a restaurant democratically. The team uses an instant runoff voting system where each member ranks all restaurant options in order of preference.
The algorithm works as follows:
Your task is to implement this voting algorithm and return the winning restaurant.
Example 1:
Input: ballots = [['A', 'B', 'C'], ['A', 'B', 'C'], ['B', 'C', 'A'], ['B', 'A', 'C'], ['C', 'A', 'B']] Output: "A" Explanation: Round 1 - First choices: A=2, B=2, C=1 C has the fewest votes and is eliminated. Round 2 - Updated ballots after removing C: Ballot 1: [A, B], Ballot 2: [A, B], Ballot 3: [B, A], Ballot 4: [B, A], Ballot 5: [A, B] First choices: A=3, B=2 A has 3/5 = 60% of votes, which exceeds 50%. A wins!
Example 2:
Input: ballots = [['X', 'Y'], ['Y', 'X'], ['X', 'Y']] Output: "X" Explanation: Round 1 - First choices: X=2, Y=1 X has 2/3 = 66.7% of votes, which exceeds 50%. X wins immediately!
Example 3:
Input: ballots = [['P', 'Q', 'R'], ['Q', 'R', 'P'], ['R', 'P', 'Q']] Output: "P" Explanation: Round 1 - First choices: P=1, Q=1, R=1 No majority. P is eliminated (alphabetically first among tied). Round 2 - First choices: Q=1, R=2 R has 2/3 = 66.7%. However, we continue... Actually, R doesn't have majority yet. Q eliminated. Round 3 - Only R remains with all votes.