본문 바로가기
CodeSignal/Arcade

CodeSignal [46/60] electionsWinners

by 벤진[Benzene] 2020. 5. 27.

Problem:

Elections are in progress!

Given an array of the numbers of votes given to each of the candidates so far, and an integer k equal to the number of voters who haven't cast their vote yet, find the number of candidates who still have a chance to win the election.

The winner of the election must secure strictly more votes than any other candidate. If two or more candidates receive the same (maximum) number of votes, assume there is no winner at all.

 

Example

For votes = [2, 3, 5, 2] and k = 3, the output should be
electionsWinners(votes, k) = 2.

  • The first candidate got 2 votes. Even if all of the remaining 3 candidates vote for him, he will still have only 5 votes, i.e. the same number as the third candidate, so there will be no winner.
  • The second candidate can win if all the remaining candidates vote for him (3 + 3 = 6 > 5).
  • The third candidate can win even if none of the remaining candidates vote for him. For example, if each of the remaining voters cast their votes for each of his opponents, he will still be the winner (the votes array will thus be [3, 4, 5, 3]).
  • The last candidate can't win no matter what (for the same reason as the first candidate).

Thus, only 2 candidates can win (the second and the third), which is the answer.

 

-Summary-

1. Find a max number in votes 

2. if any vote is greater than the max vote after adding k votes, then count the number of the possible winners.

   (Except the case that k == 0 and who has the maximum number of vote is the only one person)

모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]

https://codesignal.com/

'CodeSignal > Arcade' 카테고리의 다른 글

CodeSignal [48/60] isDigit  (0) 2020.05.27
CodeSignal [47/60] isMAC48Address  (0) 2020.05.27
CodeSignal [45/60] buildPalindrome  (0) 2020.05.27
CodeSignal [44/60] findEmailDomain  (0) 2020.05.26
CodeSignal [43/60] isBeautifulString  (0) 2020.05.26

댓글