모든 챌린지
보통 0명 해결 정답률 0%

Plurality Election

Count one vote per voter and return every candidate with the most votes.

배우게 될 내용

  • Count votes per candidate
  • Ignore invalid votes
  • Return every candidate tied for the top count

문제

번역본 보기

The student council election is a simple plurality vote: each voter writes one candidate's name, and the candidate with the most votes wins. A vote for a name that is not on the candidate list (a typo or a write-in) is thrown out. If several candidates share the highest count, they all win and share the office.

Implement plurality_election(candidates, votes).

Input

candidates: The candidates' names, all different.

votes: One name per voter. Names must match exactly, including case.

Output

Return the names of all winners in the order they appear in candidates. If no valid votes were cast, every candidate is tied at 0 and all are returned.

제약 조건

1 ≤ length of candidates ≤ 20

0 ≤ length of votes ≤ 1000

Names have 1 to 20 ASCII letters

예시

예시 1
입력:
candidates = ["Ava", "Ben", "Cruz"]
votes = ["Ben", "Ava", "Ben", "Cruz", "Ben"]

예상 출력:
expected = ["Ben"]
입력:
std::vector<std::string> candidates = {"Ava", "Ben", "Cruz"};
std::vector<std::string> votes = {"Ben", "Ava", "Ben", "Cruz", "Ben"};

예상 출력:
std::vector<std::string> expected = {"Ben"};
입력:
string[] candidates = new string[] {"Ava", "Ben", "Cruz"};
string[] votes = new string[] {"Ben", "Ava", "Ben", "Cruz", "Ben"};

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

설명

Ben has 3 votes, more than Ava and Cruz with 1 each, so ["Ben"].

예시 2
입력:
candidates = ["Ava", "Ben", "Cruz"]
votes = ["Cruz", "Ava", "ava", "Dana"]

예상 출력:
expected = ["Ava", "Cruz"]
입력:
std::vector<std::string> candidates = {"Ava", "Ben", "Cruz"};
std::vector<std::string> votes = {"Cruz", "Ava", "ava", "Dana"};

예상 출력:
std::vector<std::string> expected = {"Ava", "Cruz"};
입력:
string[] candidates = new string[] {"Ava", "Ben", "Cruz"};
string[] votes = new string[] {"Cruz", "Ava", "ava", "Dana"};

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

설명

ava and Dana are thrown out, leaving Ava and Cruz tied at 1: ["Ava", "Cruz"].

접근 방법

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

힌트 1

Start every candidate at 0 votes so candidates without votes still count.

힌트 2

Only count a vote when the name is exactly a candidate.

힌트 3

Find the highest count first, then collect everyone with that count in list order.

예상 복잡도

O(c + v) time, O(c) extra space

참고 자료

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

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

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

다른 사람의 풀이 보기

커뮤니티 솔루션

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