Skip to content

Instantly share code, notes, and snippets.

@mura303
Created February 20, 2013 13:26
Show Gist options
  • Save mura303/4995547 to your computer and use it in GitHub Desktop.
Save mura303/4995547 to your computer and use it in GitHub Desktop.
#!ruby
# http://community.topcoder.com/stat?c=problem_statement&pm=12048
#
def get_winner( events )
last_solved_index = {}
number_of_solved = Hash.new(0)
events.each_index do |i|
last_solved_index[events[i]] = i
number_of_solved[events[i]] += 1
end
max_number = number_of_solved.values.max
candidate = []
number_of_solved.each do |k,v|
if v == max_number
candidate << k
end
end
temp_winner = candidate.shift
temp_solved_index = last_solved_index[temp_winner]
candidate.each do |c|
if last_solved_index[c] < temp_solved_index
temp_winner = c
end
end
return temp_winner
end
puts get_winner( [4,7,4,1] )
# Returns: 4
puts get_winner( [10,20,30,40,50] )
# Returns: 10
puts get_winner( [123,123,456,456,456,123] )
# Returns: 456
puts get_winner( [1,2,2,3,3,3,4,4,4,4] )
# Returns: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment