Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf89a21

Browse files
1 parent5b4f618 commitdf89a21

File tree

5 files changed

+6
-6
lines changed

5 files changed

+6
-6
lines changed

‎1411/dynamic_programming/intro-to-dp.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6669,7 +6669,7 @@ <h1 id="introduction-to-dynamic-programming">Introduction to Dynamic Programming
66696669
<h2id="speeding-up-fibonacci-with-dynamic-programming-memoization">Speeding up Fibonacci with Dynamic Programming (Memoization)<aclass="headerlink"href="#speeding-up-fibonacci-with-dynamic-programming-memoization"title="Permanent link">&para;</a></h2>
66706670
<p>Our recursive function currently solves fibonacci in exponential time. This means that we can only handle small input values before the problem becomes too difficult. For instance,<spanclass="arithmatex">$f(29)$</span> results in<em>over 1 million</em> function calls!</p>
66716671
<p>To increase the speed, we recognize that the number of subproblems is only<spanclass="arithmatex">$O(n)$</span>. That is, in order to calculate<spanclass="arithmatex">$f(n)$</span> we only need to know<spanclass="arithmatex">$f(n-1),f(n-2), \dots ,f(0)$</span>. Therefore, instead of recalculating these subproblems, we solve them once and then save the result in a lookup table. Subsequent calls will use this lookup table and immediately return a result, thus eliminating exponential work!</p>
6672-
<p>Each recursive call will check against a lookup table to see if the value has been calculated. This is done in<spanclass="arithmatex">$O(1)$</span> time. If we have previouslycalcuated it, return the result, otherwise, we calculate the function normally. The overall runtime is<spanclass="arithmatex">$O(n)$</span>. This is an enormous improvement over our previous exponential time algorithm!</p>
6672+
<p>Each recursive call will check against a lookup table to see if the value has been calculated. This is done in<spanclass="arithmatex">$O(1)$</span> time. If we have previouslycalculated it, return the result, otherwise, we calculate the function normally. The overall runtime is<spanclass="arithmatex">$O(n)$</span>. This is an enormous improvement over our previous exponential time algorithm!</p>
66736673
<divclass="highlight"><pre><span></span><code><spanclass="k">const</span><spanclass="w"></span><spanclass="kt">int</span><spanclass="w"></span><spanclass="n">MAXN</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">100</span><spanclass="p">;</span>
66746674
<spanclass="kt">bool</span><spanclass="w"></span><spanclass="n">found</span><spanclass="p">[</span><spanclass="n">MAXN</span><spanclass="p">];</span>
66756675
<spanclass="kt">int</span><spanclass="w"></span><spanclass="n">memo</span><spanclass="p">[</span><spanclass="n">MAXN</span><spanclass="p">];</span>
@@ -6818,7 +6818,7 @@ <h2 id="dp-contests">DP Contests<a class="headerlink" href="#dp-contests" title=
68186818

68196819
<ulclass="metadata page-metadata"data-bi-name="page info"lang="en-us"dir="ltr">
68206820
<spanclass="contributors-text">Contributors:</span>
6821-
<ulclass="contributors"data-bi-name="contributors"><li><ahref="https://github.com/mhayter"title="mhayter"data-bi-name="contributorprofile"target="_blank">mhayter</a> (78.18%)</li><li><ahref="https://github.com/Zyad-Ayad"title="Zyad-Ayad"data-bi-name="contributorprofile"target="_blank">Zyad-Ayad</a> (9.7%)</li><li><ahref="https://github.com/clumsy"title="clumsy"data-bi-name="contributorprofile"target="_blank">clumsy</a> (5.45%)</li><li><ahref="https://github.com/ShubhamPhapale"title="ShubhamPhapale"data-bi-name="contributorprofile"target="_blank">ShubhamPhapale</a> (1.82%)</li><li><ahref="https://github.com/konstantinosalatzas"title="konstantinosalatzas"data-bi-name="contributorprofile"target="_blank">konstantinosalatzas</a> (1.21%)</li><li><ahref="https://github.com/Ahmed-Elshitehi"title="Ahmed-Elshitehi"data-bi-name="contributorprofile"target="_blank">Ahmed-Elshitehi</a> (1.21%)</li><li><ahref="https://github.com/jakobkogler"title="jakobkogler"data-bi-name="contributorprofile"target="_blank">jakobkogler</a> (1.21%)</li><li><ahref="https://github.com/tj91"title="tj91"data-bi-name="contributorprofile"target="_blank">tj91</a> (0.61%)</li><li><ahref="https://github.com/adamant-pwn"title="adamant-pwn"data-bi-name="contributorprofile"target="_blank">adamant-pwn</a> (0.61%)</li></ul>
6821+
<ulclass="contributors"data-bi-name="contributors"><li><ahref="https://github.com/mhayter"title="mhayter"data-bi-name="contributorprofile"target="_blank">mhayter</a> (78.18%)</li><li><ahref="https://github.com/Zyad-Ayad"title="Zyad-Ayad"data-bi-name="contributorprofile"target="_blank">Zyad-Ayad</a> (9.7%)</li><li><ahref="https://github.com/clumsy"title="clumsy"data-bi-name="contributorprofile"target="_blank">clumsy</a> (4.85%)</li><li><ahref="https://github.com/konstantinosalatzas"title="konstantinosalatzas"data-bi-name="contributorprofile"target="_blank">konstantinosalatzas</a> (1.82%)</li><li><ahref="https://github.com/ShubhamPhapale"title="ShubhamPhapale"data-bi-name="contributorprofile"target="_blank">ShubhamPhapale</a> (1.82%)</li><li><ahref="https://github.com/Ahmed-Elshitehi"title="Ahmed-Elshitehi"data-bi-name="contributorprofile"target="_blank">Ahmed-Elshitehi</a> (1.21%)</li><li><ahref="https://github.com/jakobkogler"title="jakobkogler"data-bi-name="contributorprofile"target="_blank">jakobkogler</a> (1.21%)</li><li><ahref="https://github.com/tj91"title="tj91"data-bi-name="contributorprofile"target="_blank">tj91</a> (0.61%)</li><li><ahref="https://github.com/adamant-pwn"title="adamant-pwn"data-bi-name="contributorprofile"target="_blank">adamant-pwn</a> (0.61%)</li></ul>
68226822
</ul>
68236823

68246824
</article>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp