<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>ganmacs</author_name>
  <author_url>https://blog.hatena.ne.jp/ganmacs/</author_url>
  <blog_title>memo-mode</blog_title>
  <blog_url>https://ganmacs.hatenablog.com/</blog_url>
  <categories>
    <anon>Ruby</anon>
    <anon>アルゴリズム</anon>
  </categories>
  <description>久しぶりにナップザック問題書いてみたら思いの外時間がかかって辛かった N = 4 W = 5 ITEMS = [[2, 3], [1, 2], [3, 4], [2, 2]].freeze # [重さ, 価値] INF = 10000000 # 全探索 def dfs(i, w) return 0 if i == N b = dfs(i + 1, w) a = w - ITEMS[i][0] &gt;= 0 ? dfs(i + 1, w - ITEMS[i][0]) + ITEMS[i][1] : 0 [a, b].max end p dfs(0, W) # =&gt; 7 # memo化 $memo =…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Fganmacs.hatenablog.com%2Fentry%2F2014%2F07%2F31%2F224106&quot; title=&quot;ナップザック問題 - memo-mode&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url></image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2014-07-31 22:41:06</published>
  <title>ナップザック問題</title>
  <type>rich</type>
  <url>https://ganmacs.hatenablog.com/entry/2014/07/31/224106</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
