Advertisement
brewersfan1976

elections_winners.rb

Aug 29th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.50 KB | None | 0 0
  1. def electionsWinners(votes, k)
  2.     maximum = votes.max
  3.     array_length = votes.length
  4.     x = 0
  5.     winners = 0
  6.     votes = votes.sort
  7.     votes = votes.reverse
  8.    
  9.     if k == 0 then
  10.        if votes[0] == votes[1] then
  11.           return 0
  12.        else
  13.           return 1
  14.        end
  15.     end
  16.    
  17.     while (x < array_length)
  18.           if votes[x] + k > maximum then
  19.              winners = winners + 1
  20.           end
  21.              
  22.           x = x + 1
  23.     end
  24.        
  25.     return winners
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement