Created
November 15, 2020 20:09
-
-
Save msg7086/ce899c87ea7e72b790d03d85794ba2ee 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
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