Skip to content

Instantly share code, notes, and snippets.

@anildigital
Created December 29, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anildigital/69673488a1b63779bbd5 to your computer and use it in GitHub Desktop.
Save anildigital/69673488a1b63779bbd5 to your computer and use it in GitHub Desktop.
def select(total_talk_time, talk_list)
time = 0
selected_talk_list = []
original_talk_list = talk_list.clone
while time < total_talk_time
if talk_list.empty?
break
end
min = talk_list.min
if (min + time) <= total_talk_time
selected_talk_list << min
talk_list.slice!(talk_list.index(min))
end
time = time + min
end
selected_talk_list
selected_talk_list_size = selected_talk_list.size
original_talk_list.combination(selected_talk_list_size).select { |ns| ns.reduce(:+) <= total_talk_time }.map { |e| e.sort! }.uniq.sort.reverse.first
end
if __FILE__ == $0
talk_list = [30, 40, 20, 20, 30]
total_talk_time = 120
puts select(total_talk_time, talk_list).inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment