Created
March 25, 2013 04:13
-
-
Save atomaka/5234877 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def reader file | |
problems = file.readline.to_i | |
problems.times do | |
numbers_count = file.readline.to_i | |
numbers = Array.new | |
(1..numbers_count).each do | |
numbers << file.readline.to_i | |
end | |
p solver(numbers) | |
end | |
end | |
def solver(numbers) | |
max_sum = numbers.inject(:+) | |
max_buckets = 0 | |
# loop through all possible sums in set | |
(1..max_sum).each do |sum| | |
set = Array.new | |
buckets = 0 | |
# go through all of our numbers | |
numbers.each do |i| | |
set << i | |
# if it is larger than the current | |
while set.length > 0 && set.inject(:+) > sum | |
# remove first element | |
set.shift | |
end | |
# if we found matching set | |
if set.inject(:+) == sum | |
# mark it and start over | |
buckets += 1 | |
set = [] | |
end | |
end | |
# if its the new max, save results | |
max_buckets = buckets if buckets > max_buckets | |
end | |
max_buckets | |
end | |
reader(STDIN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment