Đề bài
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.
- 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.
- 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.
- 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.
Ràng buộc
1 ≤ length of candidates ≤ 9
0 ≤ number of ballots ≤ 200
Ví dụ
Đầu vào:
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]
]
Kết quả mong đợi:
expected = ["Cal"]
Đầu vào:
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}
};
Kết quả mong đợi:
std::vector<std::string> expected = {"Cal"};
Đầu vào:
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}
};
Kết quả mong đợi:
string[] expected = new string[] {"Cal"};
Giải thích
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"].
Đầu vào:
candidates = ["Ann", "Bob"]
ballots = [
[0, 1],
[1, 0]
]
Kết quả mong đợi:
expected = ["Ann", "Bob"]
Đầu vào:
std::vector<std::string> candidates = {"Ann", "Bob"};
std::vector<std::vector<long long>> ballots = {
{0, 1},
{1, 0}
};
Kết quả mong đợi:
std::vector<std::string> expected = {"Ann", "Bob"};
Đầu vào:
string[] candidates = new string[] {"Ann", "Bob"};
long[][] ballots = new long[][] {
new long[] {0, 1},
new long[] {1, 0}
};
Kết quả mong đợi:
string[] expected = new string[] {"Ann", "Bob"};
Giải thích
Ann and Bob are tied 1 to 1, so no arrow is locked and both are returned: ["Ann", "Bob"].
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
For every ballot, each candidate is preferred over everyone ranked after them.
Gợi ý 2
Before locking winner to loser, search from the loser along locked arrows; if you reach the winner, skip it.
Gợi ý 3
At the end, a winner is a candidate with no incoming locked arrow.
Độ phức tạp mong đợi
O(b × c^2 + c^4) time, O(c^2) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.