<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ravi's Blog]]></title><description><![CDATA[Ravi's Blog]]></description><link>https://blog.ravimashru.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 05 Jun 2026 19:35:23 GMT</lastBuildDate><atom:link href="https://blog.ravimashru.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[A Brief Introduction to Dynamic Programming]]></title><description><![CDATA[Dynamic programming is a technique that can be used to solve a particular class of problems. Let's take a look at how to determine if you can use dynamic programming for a given problem, and the different approaches (top-down and bottom-up) that you ...]]></description><link>https://blog.ravimashru.dev/a-brief-introduction-to-dynamic-programming</link><guid isPermaLink="true">https://blog.ravimashru.dev/a-brief-introduction-to-dynamic-programming</guid><category><![CDATA[data structures]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Ravi Mashru]]></dc:creator><pubDate>Thu, 01 Jul 2021 04:29:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1626375269295/yqBrhoe-0.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Dynamic programming is a technique that can be used to solve a particular class of problems. Let's take a look at how to determine if you can use dynamic programming for a given problem, and the different approaches (top-down and bottom-up) that you can use.</p>
<h2 id="when-can-you-use-dynamic-programming">When can you use dynamic programming?</h2>
<p>To use dynamic programming, you need to be able to break down the problem into smaller subproblems. If you are sure you can do that, then you need to check if the problem has the following properties: </p>
<ol>
<li>Overlapping subproblems </li>
<li>Optimal substructure </li>
</ol>
<p>If a problem can be broken down into smaller subproblems and has these two properties, then you can apply dynamic programming to solve the problem and <strong>success is guaranteed</strong>!</p>
<p>Let's dive deeper into what each of these mean while trying to solve <a target="_blank" href="https://leetcode.com/problems/climbing-stairs/">problem #70</a> - Climbing Stairs on Leetcode. </p>
<p>Here is what the problem statement says: </p>
<blockquote>
<p>You are climbing a staircase. It takes <code>n</code> steps to reach the top. </p>
<p>Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?</p>
</blockquote>
<h3 id="break-down-the-problem">Break down the problem</h3>
<p>Let us first see if we can break down the problem into smaller subproblems. Let us say that the answer we want - the number of distinct ways we can climb a staircase with <code>n</code> steps, can be expressed as <code>countDistinctPaths(n)</code>. </p>
<p>Now, we know that we can take either one or two steps at a time. If we take one step, we need to follow the same rules to climb the remaining <code>n-1</code> steps. Similarly, if we climb two steps, we again need to follow the same rules to climb the remaining <code>n-2</code> steps. </p>
<p>So, the total number of ways we can climb the staircase is either by taking one step and then taking one of the <code>countDistinctPaths(n-1)</code> paths for the remaining <code>n-1</code> steps, or by taking two steps and then taking one of the <code>countDistinctPaths(n-2)</code> paths for the remaining <code>n-2</code> steps. </p>
<p>We can write the total number of paths we can take as follows:</p>
<p><code>countDistinctPaths(n) = countDistinctPaths(n-1) + countDistinctPaths(n-2)</code></p>
<p>As you can see, we've managed to break the problem down into smaller subproblems! If we have the answer for <code>n-1</code> and <code>n-2</code>, then we can combine those answers to calculate the answer for <code>n</code>. </p>
<p>There's one more thing we need to think about though - how small can we keep making the problems? </p>
<p>Well, the smallest staircase we can have is one with a single step. And there is only one way we can climb that step (since we can't take two steps in this case - because there's only a single step!) Also, if there is no staircase at all, there is no way we can climb it! </p>
<p>So we can rewrite the problem as: </p>
<p><code>countDistinctPaths(0) = 0</code> (no staircase, so no way to climb it!) </p>
<p><code>countDistinctPaths(1) = 1</code> (only one step, only one way to climb it) </p>
<p>For any value of n greater than 1, <code>countDistinctPaths(n) = countDistinctPaths(n-1) + countDistinctPaths(n-2)</code></p>
<p>This kind of expression is also commonly known as a <strong>recurrence relation</strong>. </p>
<h3 id="overlapping-subproblems">Overlapping subproblems</h3>
<p>To check if there are overlapping subproblems, let us try to think about how many times we will call <code>countDistinctPaths</code> if we want the answer of say a staircase with 5 steps.</p>
<p>We know that the answer we're looking for is <code>countDistinctPaths(5)</code>. </p>
<p>From our recurrence relation, we know that <code>countDistinctPaths(5) = countDistinctPaths(4) + countDistinctPaths(3)</code>. </p>
<p>We can then use the recurrence relation to further break down <code>countDistinctPaths(4)</code> into <code>countDistinctPaths(4) = countDistinctPaths(3) + countDistinctPaths(2)</code>. </p>
<p>Now if we put this back in the first expression, we get <code>countDistinctPaths(5) = (countDistinctPaths(3) + countDistinctPaths(2)) + countDistinctPaths(3)</code>.</p>
<p>Similarly, we can replace <code>countDistinctPaths(3)</code> with <code>countDistinctPaths(2) + countDistinctPaths(1)</code>.</p>
<p>The result is then <code>countDistinctPaths(5) = (countDistinctPaths(3) + countDistinctPaths(2)) + (countDistinctPaths(2) + countDistinctPaths(1))</code>.</p>
<p>If you look closely, you'll notice that we're computing <code>countDistinctPaths(3)</code> and <code>countDistinctPaths(2)</code> multiple times. </p>
<p>It's easier to see all the computations we have to do in the form of a tree: </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1626375267807/q_pYJhHZB0.png" alt="Call tree of a recursive function solving a problem with overlapping subproblems" /></p>
<p>Each node in this tree is a call to <code>countDistinctPaths</code> and the value in the node is the parameter we are passing to the function. As you can see, we're repeating the calls for 2 and 3. This tells us that the problem we are trying to solve has overlapping subproblems. </p>
<h3 id="optimal-substructure">Optimal substructure</h3>
<p>If you can find the optimal solution to a problem using optimal solutions to its subproblems, then the problem is said to have an optimal substructure. </p>
<p>What this means for us is that to find the optimal solution for <code>countDistinctPaths(n)</code>, we need the optimal solution for <code>countDistinctPaths(n-1)</code> and <code>countDistinctPaths(n-2)</code>. In this particular context, "optimal" for us means the maximum number of paths. Therefore, this problem has an optimal substructure. </p>
<p>I personally had a tough time understanding this property and found looking at examples of problems that DON'T have optimal substructure helped understand this better. You can find a list of such problems on the <a target="_blank" href="https://en.wikipedia.org/wiki/Optimal_substructure#Problems_without_optimal_substructure">optimal substructure page on Wikipedia</a>.</p>
<h2 id="how-can-you-apply-dynamic-programming-to-a-problem">How can you apply dynamic programming to a problem?</h2>
<p>Once you have verified that a problem can be solved using dynamic programming, there are two approaches you can use to solve it: the <strong>top-down</strong> approach or the <strong>bottom-up approach</strong>. Let's take a closer look at each of these. </p>
<h3 id="the-top-down-approach">The top-down approach</h3>
<p>The top-down approach involves converting the recurrence relation we wrote to recursive function calls and then making some minor tweaks to prevent the repeated evaluation of the overlapping subproblems. </p>
<p>Let's start with a recursive implementation of our recurrence relation in JavaScript:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">countDistinctPaths</span>(<span class="hljs-params">n</span>) </span>{ 

  <span class="hljs-comment">// If there are no stairs </span>
  <span class="hljs-keyword">if</span> (n === <span class="hljs-number">0</span>) { 
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
  } 

  <span class="hljs-comment">// If there is only one stair </span>
  <span class="hljs-keyword">if</span> (n === <span class="hljs-number">1</span>) { 
    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>; 
  } 

  <span class="hljs-comment">// The recurrence relation </span>
  <span class="hljs-keyword">return</span> countDistinctPaths(n<span class="hljs-number">-1</span>) + countDistinctPaths(n<span class="hljs-number">-2</span>); 

}
</code></pre>
<p>As we saw in the tree above, this plain recursive implementation makes repeated calculations for the overlapping subproblems. These repeated calls mean we are spending time calculating the same values over and over again. And the number of repeated calls is much higher for larger values of <code>n</code> so we're wasting a lot of time!</p>
<p>With dynamic programming, we can compute the value of each subproblem just once and store it in memory - a technique called "memoization" (nope, not a typo, it's not memorization. See <a target="_blank" href="https://en.wikipedia.org/wiki/Memoization">this Wikipedia page</a> for how this term was coined). The next time we encounter the same subproblem and need to calculate the value, instead of using the recurrence relation, we can just retrieve the result from memory!</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// We've added "memo"ry to store values we compute. It is empty in the beginning. </span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">countDistinctPaths</span>(<span class="hljs-params">n, memo={}</span>) </span>{ 

  <span class="hljs-comment">// If there are no stairs </span>
  <span class="hljs-keyword">if</span> (n === <span class="hljs-number">0</span>) { 
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; 
  } 

  <span class="hljs-comment">// If there is only one stair </span>
  <span class="hljs-keyword">if</span> (n === <span class="hljs-number">1</span>) { 
    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>; 
  } 

  <span class="hljs-comment">// Check if we have already computed this before </span>
  <span class="hljs-keyword">if</span> (n <span class="hljs-keyword">in</span> memo) { 
    <span class="hljs-keyword">return</span> memo[n]; 
  } 

  <span class="hljs-comment">// The recurrence relation with two minor tweaks: </span>
  <span class="hljs-comment">// 1. We store the computed value in memo[n] for future use </span>
  <span class="hljs-comment">// 2. We pass the memory object to the recursive calls </span>
  <span class="hljs-keyword">return</span> memo[n] = countDistinctPaths(n<span class="hljs-number">-1</span>, memo) + countDistinctPaths(n<span class="hljs-number">-2</span>, memo); 

}
</code></pre>
<p>This approach is called top-down because we start with the biggest problem first, <code>countDistinctPaths(n)</code> and keep recursively breaking it down into smaller problems until we reach the smallest possible subproblems - <code>countDistinctPaths(0)</code> and <code>countDistinctPaths(1)</code>. </p>
<h3 id="the-bottom-up-approach">The bottom-up approach</h3>
<p>With the bottom up approach, we start from the smallest subproblems and iteratively combine them until we find a solution to the original problem. </p>
<p>For us, this means we start with <code>countDistinctPaths(0)</code> and <code>countDistinctPaths(1)</code>to which we know the answer, and then combine them to get the answer to <code>countDistinctPaths(2)</code> and then <code>countDistinctPaths(3)</code> and so on until we find the answer to <code>countDistinctPaths(n)</code>. </p>
<p>We need a way to store the value of <code>countDistinctPaths(n)</code> for each value of <code>n</code>. This storage is commonly known as a "table" and this bottom-up approach is also commonly called "tabulation". </p>
<p>As you can see, there is no recursion involved here. Just good old iteration! </p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">countDistinctPaths</span>(<span class="hljs-params">n</span>) </span>{ 

  <span class="hljs-comment">// Our "table" to store the result for each value of n</span>
  <span class="hljs-keyword">const</span> table = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(n + <span class="hljs-number">1</span>); 

  <span class="hljs-comment">// The case with no stairs </span>
  table[<span class="hljs-number">0</span>] = <span class="hljs-number">0</span>; 

  <span class="hljs-comment">// The case with one stair </span>
  table[<span class="hljs-number">1</span>] = <span class="hljs-number">1</span>; 

  <span class="hljs-comment">// We keep combining subproblems until we find a solution to the original problem </span>
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">2</span>; i &lt;= n; i++) { 
    table[i] = table[i<span class="hljs-number">-1</span>] + table[i<span class="hljs-number">-2</span>]; 
  } 

  <span class="hljs-keyword">return</span> table[n]; 
}
</code></pre>
<p>And that's it! Once the loop finishes, we'll have the result we need at index <code>n</code> of the table. </p>
<h2 id="how-can-you-learn-more-about-dynamic-programming">How can you learn more about dynamic programming?</h2>
<p>Like the title of this post says, this was just a brief introduction dynamic programming. The example I chose to work with was quite simple. There will be more complicated questions in which either the recurrence relation is not very obvious, or you need a two or even three dimensional table. The only way to get used to identifying if you can use dynamic programming to solve a particular problem and if so, how you can break down the problem and identify the key components is by practice. </p>
<p>To practice problems, I highly recommend LeetCode. Here's a <a target="_blank" href="https://leetcode.com/tag/dynamic-programming/">list of all the dynamic programming problems on LeetCode</a>. LeetCode has great quality questions for every topic. But my personal favorite feature on LeetCode is the <strong>Discussions</strong> tab in each problem. After you've solved the problem you can see and discuss how others are solving the same problem. Learning from other, more experienced programmers has always worked great for me. </p>
<p>So go ahead and start solving problems! Even better, make a challenge out of it! I'm currently doing a "100 Days of Code" challenge (which I log in <a target="_blank" href="https://github.com/ravimashru/100-days-of-code">this repo</a> on GitHub) where I try to spend at least an hour every day solving problems on LeetCode or other similar sites. </p>
]]></content:encoded></item></channel></rss>