Last active
August 29, 2015 14:23
-
-
Save konk303/c9ae5c1a8af1bbae601e to your computer and use it in GitHub Desktop.
https://codeiq.jp//challenge/1618 with set
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 | |
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