Skip to content

Instantly share code, notes, and snippets.

@konk303 konk303/solve.rb
Last active Aug 29, 2015

Embed
What would you like to do?
#!/usr/bin/env ruby
target = gets.to_i
count = gets.to_i
values = {}
count.times do |i|
values[gets.to_i] = 1
end
answer = 0
values_a = values.keys.sort
values_a.each_with_index do |vi, i|
values_a[i.succ, count].each do |vj|
need = target - vi - vj
# sortしてるので、`3つめより2つめが大きいか同じ == これ以上先に進んでも満たせる可能性がない`
break if vj >= need
answer = answer.succ if values[need]
end
end
puts answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.