Skip to content

Instantly share code, notes, and snippets.

@inage
Created December 7, 2013 14:52
Show Gist options
  • Select an option

  • Save inage/7843514 to your computer and use it in GitHub Desktop.

Select an option

Save inage/7843514 to your computer and use it in GitHub Desktop.
個数制限なしナップサック問題
## 個数制限なしナップサック問題
## http://rubyfiddle.com/riddles/9eb7a
n = 3
w = [3,4,2]
v = [4,5,3]
W = 7
dp = Array.new(W+1,0)
i=j=0
n.times{|i|
j = w[i]
while j <= W
dp[j] = [dp[j],dp[j-w[i]]+v[i]].max
j+=1
end
}
print dp[W]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment