Skip to content

Instantly share code, notes, and snippets.

@msg7086
Created November 15, 2020 20:09
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 msg7086/ce899c87ea7e72b790d03d85794ba2ee to your computer and use it in GitHub Desktop.
Save msg7086/ce899c87ea7e72b790d03d85794ba2ee to your computer and use it in GitHub Desktop.
def combination_sum(candidates, target)
dp = Array.new(target+1) {Array.new}
candidates.each do |n|
n.upto(target) do |t|
next dp[t] << [n] if n == t
dp[t] += dp[t - n].map {|comb| comb.dup + [n]}
end
end
dp[target]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment