Skip to content

Commit df89a21

Browse files
1 parent 5b4f618 commit df89a21

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
<h2 id="speeding-up-fibonacci-with-dynamic-programming-memoization">Speeding up Fibonacci with Dynamic Programming (Memoization)<a class="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, <span class="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 <span class="arithmatex">$O(n)$</span>. That is, in order to calculate <span class="arithmatex">$f(n)$</span> we only need to know <span class="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 <span class="arithmatex">$O(1)$</span> time. If we have previously calcuated it, return the result, otherwise, we calculate the function normally. The overall runtime is <span class="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 <span class="arithmatex">$O(1)$</span> time. If we have previously calculated it, return the result, otherwise, we calculate the function normally. The overall runtime is <span class="arithmatex">$O(n)$</span>. This is an enormous improvement over our previous exponential time algorithm!</p>
66736673
<div class="highlight"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">MAXN</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span>
66746674
<span class="kt">bool</span><span class="w"> </span><span class="n">found</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span>
66756675
<span class="kt">int</span><span class="w"> </span><span class="n">memo</span><span class="p">[</span><span class="n">MAXN</span><span class="p">];</span>
@@ -6818,7 +6818,7 @@ <h2 id="dp-contests">DP Contests<a class="headerlink" href="#dp-contests" title=
68186818

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