<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Utkarshnagdev on Medium]]></title>
        <description><![CDATA[Stories by Utkarshnagdev on Medium]]></description>
        <link>https://medium.com/@utkarsh2nagdev?source=rss-35d054a4fadd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nAUT9hNpuFgT3_v3GEbgzA.jpeg</url>
            <title>Stories by Utkarshnagdev on Medium</title>
            <link>https://medium.com/@utkarsh2nagdev?source=rss-35d054a4fadd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 08:00:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@utkarsh2nagdev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Conquering Dynamic Programming as a beginner—an unorthodox guide]]></title>
            <link>https://medium.com/@utkarsh2nagdev/conquering-dynamic-programming-as-a-beginner-an-unorthodox-guide-96c225c8f37?source=rss-35d054a4fadd------2</link>
            <guid isPermaLink="false">https://medium.com/p/96c225c8f37</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[guides-and-tutorials]]></category>
            <dc:creator><![CDATA[Utkarshnagdev]]></dc:creator>
            <pubDate>Tue, 07 Sep 2021 17:30:28 GMT</pubDate>
            <atom:updated>2021-10-02T14:40:06.883Z</atom:updated>
            <content:encoded><![CDATA[<h4>How I found my way around classic questions of Dynamic Programming.</h4><p>I’ve seen that most of my peers and juniors I know, fear Dynamic programming. A few weeks ago, this list would’ve included me too. I tried learning dynamic programming with a companion, a DSA beginner to check out my theory, which stated “<strong>Is the fear of DP valid or not?”</strong>. I’ve concluded that the fear isn’t completely valid. Although some tough questions are difficult to comprehend for a beginner, most questions follow a pattern and it gets really easy with practice. The biggest factor in dynamic programming is preparedness and not complex problem-solving.</p><blockquote>Throughout the post, we will use the example of Fibonacci series which states that each number is the sum of the two preceding ones, starting from 0 and 1.</blockquote><blockquote>Also I have used the words, <strong>sub-problems</strong> and <strong>state</strong> interchangeably depending on the context.</blockquote><p><strong>What is Dynamic Programming? How to recognize it?</strong></p><p>Dynamic Programming is a very useful way of improving the performance of a solution that follows two conditions :</p><ol><li><strong>Overlapping Subproblem:</strong> Problems that repeat should not be solved again, instead their solutions should be stored in memory and used whenever they’re repeated.</li><li><strong>Optimal Substructure Property:</strong> Bigger problems (example: length of n) can be derived from previous smaller sub-problems (smaller than n).</li></ol><blockquote>Both properties can be seen below for Fibonacci series.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/1*HQmk0oJglH1HXVpWw1nOhQ.png" /><figcaption>Repeated subproblems are coloured the same(1), Bigger problems can be solved from smaller sub-problems(2)</figcaption></figure><p>Whenever you recognize these two conditions in a problem, use Dynamic Programming. It might be difficult at the start, but as time goes on you’ll recognize them faster. <strong>Remember DP is about preparedness and not complex solving. <br></strong>Try to see in a problem if DP fits, especially if the problem is like “find all the ways to do the something”, “ best possible path out of many paths” or just repetitive sub-problem dependencies.</p><p><strong>How to solve them?<br></strong>Prerequisites: Understanding of <strong>Recursion</strong>. Having a good grasp of recursion techniques will really help you in DP.</p><p>DP is an optimization technique for repetitive sub-problems and optimal substructure problems.</p><p>To optimize a solution, first, we require one. So find a brute force solution. Again I <em>emphasize</em>, find a brute force solution. For me, I write a clean-recursive solution that can be later optimized.</p><p>Follow the following steps to find a recursive problem then optimize it by using dynamic programming.</p><h3><strong>1. Identify unique variables to each state.</strong></h3><p>We know there is a recursive structure to our problem, which means some variables will be changing across the recursive calls(sub-problems). Let’s call each recursive call a <strong>state</strong>. So recognize in each state what variables keep changing. Generally 1, 2 or 3 variables change depending on the type of the problem. <strong>Basically, in each state what are the few values that represent the state uniquely.</strong></p><blockquote>Example: To find the Fibonacci n-th number, we can see that each state is dependent on previous states. But each state is represented by the value of <strong>n </strong>only. Each <strong>Fib(n)</strong> has only one variable <strong>n</strong>, which is unique for that state. Here there is 1 variable per state. For Fib(5), 5 is what represents that sub-problem.</blockquote><h3><strong>2. Write a recursion relation</strong>.</h3><p>Basically, a relation between<strong> the current state</strong>, and <strong>sub-problems of the current state</strong>.</p><blockquote>Example: For the Fibonacci series, the recurrence relation is <br><strong>Fib(n) = Fib(n-1) + Fib(n-2).<br>Fib(5) = Fib(4) + Fib(3)</strong></blockquote><h3><strong>3. Identify the base cases.</strong></h3><p>Base cases are the <strong>starting </strong>points of a recurrence. There will be a stage where we won’t have sub-problems to get a result instead it will have a constant value for it.</p><blockquote>Example: For the Fibonacci series, base cases are <strong>Fib(0) = 0, Fib(1) = 1,<br></strong>that is for <strong><em>n &lt; 2 F(n)</em></strong><em> = 1.</em></blockquote><h3>4. Optimize the solution</h3><p>Since we have now implemented recursion for the problem, we want to optimize it. During recursion we used <strong>optimal substructure </strong>to solve the problem, now to improve the speed of the solution we will use <strong>overlapping subproblem</strong> property of the solution. Overlapping subproblem means there are smaller subproblems which we are solving repeatedly. We want to stop doing this repeated work. We can do this by storing results. This process is called <strong>Memoization.</strong></p><h3><strong>How to use memoization?</strong></h3><p>When we were creating the recursive solution, we found out the <strong>unique variables</strong> that represent each state/subproblem. We need to store solutions of those unique states by using arrays/hashmaps, where the <strong>index or keys</strong> are the unique state variables that <strong>represent that subproblem</strong> and the <strong>value</strong> stored in that is the <strong>solution</strong> to that subproblem. Whenever a repeated subproblem comes up, we just check if this subproblem is already solved and return that value.</p><blockquote>For example, to solve Fibonacci series for 5, we will create an array of 6 members (starting from 0 to 5). Whenever we solve Fib(3) we will store that solution in array[3]. So whenever a call for Fib(3) comes up we just return value stored in array[3].</blockquote><p>Now we have <strong>drastically</strong> improved the solution.</p><blockquote>For example, When you solve Fibonacci<strong> without Memoization</strong>, the time complexity is <strong>O(2^n)</strong> vs <strong>with memoization</strong> the time complexity drops directly to <strong>O(n)</strong>. This is a huge huge improvement to the problem.</blockquote><h3>Top-Down and Bottom-Up approach</h3><p>We used a recursive approach that began with a larger problem but was decomposed into several subproblems, yielding the original problem’s solution. This is called the <strong>Top-Down </strong>approach.</p><p>When we turn around the approach and start solving the smallest sub-problem and <strong>iteratively</strong> reach the original problem, that is called the <strong>Bottom-Up</strong> approach. When we use this method, we tabulate the current subproblem from smaller subproblems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*9pmlO8DYtK5e84Js1Bpb6Q.png" /></figure><h3>Ending note</h3><p>We’re done with the theory. Start practising. <strong>Remember,</strong> Dynamic programming is about practice and not complex problem-solving. Best of luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96c225c8f37" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>