모든 챌린지
어려움 0명 해결 정답률 0%

Ranked Pairs Winner

Find the ranked pairs winner of an election by locking head-to-head victories that do not make a cycle.

배우게 될 내용

  • Build a head-to-head preference table from ranked ballots
  • Sort victories by margin with exact tie rules
  • Lock edges only when they keep the graph acyclic

문제

번역본 보기

Ranked pairs picks a winner by looking at every head-to-head matchup. Every voter ranks all the candidates, and candidate a beats candidate b when more voters rank a above b than b above a. The margin of that victory is (voters preferring a) − (voters preferring b). Matchups where both sides have the same count are left out.

  1. Sort the victories by margin, largest first. Equal margins are ordered by the winner's number, smaller first, and then by the loser's number, smaller first.
  2. Go through the victories in that order and lock each one in as an arrow from winner to loser, unless that arrow would close a cycle with arrows already locked (that is, unless the loser can already reach the winner by following locked arrows). Such a victory is skipped.
  3. The winners are the candidates that no locked arrow points to.

Implement ranked_pairs_winner(candidates, ballots).

Input

candidates: The candidates' names. Candidate i is candidates[i].

ballots: One row per voter: the candidate numbers (0-based) from most preferred to least preferred. Each row lists every candidate exactly once.

Output

Return the names of all candidates with no locked arrow pointing to them, in the order they appear in candidates. Usually this is a single name; it can be several when some matchups were tied, and with no ballots it is every candidate.

제약 조건

1 ≤ length of candidates ≤ 9

0 ≤ number of ballots ≤ 200

예시

예시 1
입력:
candidates = ["Ann", "Bob", "Cal"]
ballots = [
    [0, 1, 2],
    [0, 1, 2],
    [0, 1, 2],
    [1, 2, 0],
    [1, 2, 0],
    [2, 0, 1],
    [2, 0, 1],
    [2, 0, 1],
    [2, 0, 1]
]

예상 출력:
expected = ["Cal"]
입력:
std::vector<std::string> candidates = {"Ann", "Bob", "Cal"};
std::vector<std::vector<long long>> ballots = {
    {0, 1, 2},
    {0, 1, 2},
    {0, 1, 2},
    {1, 2, 0},
    {1, 2, 0},
    {2, 0, 1},
    {2, 0, 1},
    {2, 0, 1},
    {2, 0, 1}
};

예상 출력:
std::vector<std::string> expected = {"Cal"};
입력:
string[] candidates = new string[] {"Ann", "Bob", "Cal"};
long[][] ballots = new long[][] {
    new long[] {0, 1, 2},
    new long[] {0, 1, 2},
    new long[] {0, 1, 2},
    new long[] {1, 2, 0},
    new long[] {1, 2, 0},
    new long[] {2, 0, 1},
    new long[] {2, 0, 1},
    new long[] {2, 0, 1},
    new long[] {2, 0, 1}
};

예상 출력:
string[] expected = new string[] {"Cal"};

설명

Cal beats Ann 6 to 3 (margin 3), Ann beats Bob 7 to 2 (margin 5) and Bob beats Cal 5 to 4 (margin 1). Lock Ann over Bob, then Cal over Ann; Bob over Cal would close a cycle, so it is skipped. Nothing points to Cal: ["Cal"].

예시 2
입력:
candidates = ["Ann", "Bob"]
ballots = [
    [0, 1],
    [1, 0]
]

예상 출력:
expected = ["Ann", "Bob"]
입력:
std::vector<std::string> candidates = {"Ann", "Bob"};
std::vector<std::vector<long long>> ballots = {
    {0, 1},
    {1, 0}
};

예상 출력:
std::vector<std::string> expected = {"Ann", "Bob"};
입력:
string[] candidates = new string[] {"Ann", "Bob"};
long[][] ballots = new long[][] {
    new long[] {0, 1},
    new long[] {1, 0}
};

예상 출력:
string[] expected = new string[] {"Ann", "Bob"};

설명

Ann and Bob are tied 1 to 1, so no arrow is locked and both are returned: ["Ann", "Bob"].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

For every ballot, each candidate is preferred over everyone ranked after them.

힌트 2

Before locking winner to loser, search from the loser along locked arrows; if you reach the winner, skip it.

힌트 3

At the end, a winner is a candidate with no incoming locked arrow.

예상 복잡도

O(b × c^2 + c^4) time, O(c^2) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.