<?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 Archita on Medium]]></title>
        <description><![CDATA[Stories by Archita on Medium]]></description>
        <link>https://medium.com/@archita1102?source=rss-1b1f22caadcd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KO8XBi6vCCa3UNM6</url>
            <title>Stories by Archita on Medium</title>
            <link>https://medium.com/@archita1102?source=rss-1b1f22caadcd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:04:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@archita1102/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[Mastering Dynamic Programming (DP)]]></title>
            <link>https://medium.com/@archita1102/mastering-dynamic-programming-dp-525bf74ecf26?source=rss-1b1f22caadcd------2</link>
            <guid isPermaLink="false">https://medium.com/p/525bf74ecf26</guid>
            <dc:creator><![CDATA[Archita]]></dc:creator>
            <pubDate>Sat, 04 Jan 2025 11:39:44 GMT</pubDate>
            <atom:updated>2025-01-04T12:26:39.046Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Dynamic Programming:</h3><h4>Code Smarter, Not Harder</h4><h4><strong>Overview:</strong></h4><p>Here in this blog we are going to learn:</p><p>· What exactly is DP</p><p>· Pre-requisite for DP</p><p>· Where to use it</p><p>· The approaches to use DP via a level1 and level2 questions</p><p>In today’s fast-paced tech world, Data Structures and Algorithms (DSA) have become the bread and butter for every coder. Why? Because mastering DSA sharpens your logic-building skills — an absolute must in the coding industry. While coming up with an approach to solve a problem may seem straightforward, the real battle lies in pushing the solution to its limits — optimizing both time and space complexity. After all, these are the two pillars that hold the foundation of DSA.</p><p>Now, when it comes to optimization, one of the coolest and most powerful techniques is Dynamic Programming (DP). Let’s dive into what makes it so special!</p><p><strong>What Makes DP Special?</strong></p><p>Dynamic Programming (DP) simply says <strong><em>stop reinviting the wheel</em></strong> as it is a clever way to tackle problems where recursion ends up doing the same calculations repeatedly. Instead of wasting time recalculating results, DP lets us store solutions to subproblems and reuse them whenever needed. This drastically cuts down the time complexity to O(n) — a sweet, linear-time solution.</p><p>DP can be split into two main approaches:</p><p>· <strong>Memoization (Top-Down)</strong> — Think of it as recursion with a memory boost.</p><p>· <strong>Tabulation (Bottom-Up)</strong> — An iterative and table-filling approach.</p><p><strong>Pre-requisite to solve a question via DP:</strong></p><p>· Recursion</p><p>· Vectors</p><p>· Understanding of breaking a problem into subproblems</p><p><strong>Identify where to use it:</strong></p><p>· Where you see repetition in subproblems.</p><p>· You have to maximize or minimize a problem.</p><p>· You have to give a count of particular item or cases.</p><p><strong>Example Problem:-</strong></p><p><strong><em>LEVEL1</em>: Fibonacci Series</strong></p><p>Let’s take a classic example — the Fibonacci series. Imagine you want to find the nth number in the sequence using recursion. While the logic may look simple, it hides a big flaw — redundant calculations. The same values are recalculated over and over for each new element in the series.</p><pre>int fibonacci(int n)<br>{<br>  if(n==1||n==0)<br>  {<br>    return n;<br>  }<br>  return fibonacci(n-1)+fibonacci(n-2);<br>}</pre><p>This approach quickly spirals into inefficiency as n grows. But don’t worry — DP is here to save the day!</p><p><strong>Steps to Optimize Using Memoization:</strong></p><p>· Create a DP array: Declare a 1D array fibo[] of size (n+1) and initialize all entries to -1.</p><p>· Store computed values: Whenever we calculate a Fibonacci number, stash it in the array at its respective index.</p><p>· Reuse values: Instead of recomputing values, simply fetch precomputed results from the array.</p><pre>int fibonacci(int n, vector&lt;int&gt; fibo)<br>{<br>   if(n==1||n==0)<br>  {<br>    return n;<br>  }<br>  if(fibo[n]!=-1)<br>  {<br>    return fibo[n];<br>  }<br>  fibo[n]=fibonacci[n-1]+fibonacci[n-2];<br>}</pre><p>Boom! With just a few more steps, we’ve removed all unnecessary calculations and built a much faster, smarter solution.</p><p><strong>Solving with Tabulation:</strong></p><p>· Create a DP array: Declare a 1D array fibo[] of size (n+1) and initialize all entries to -1.</p><p>· Using a loop we store the calculated values at their respective indexes</p><p>· Then in the end reuse it for calculation in further steps</p><pre>int fib(n)<br>{<br>  fibo = [0] * (n + 1)<br>  fibo[1] = 1<br>  for(int i=2; i&lt;n+1; i++)<br>  {<br>    fibo[i] = fibo[i - 1] + fibo[i - 2]<br>  }  <br>  return fibo[n]<br>}</pre><p><strong><em>LEVEL2</em>: Knapsack Problem</strong></p><p>Now here’s another famous problem — The Knapsack Problem. The problem states that we are given weights and a value n ans we need to determine the maximum value you can obtain within a weight limit w.</p><p>Approach:</p><p> <strong>Initialization:</strong></p><ul><li>n: The total number of items (wt.size()).</li><li>dp: A 2D table of size (n+1) x (W+1) to store the maximum value for each subproblem.</li><li>dp[i][w]: The maximum value achievable using the first i items with a weight limit of w.</li></ul><p> <strong>Base Cases (First Row and Column):</strong></p><ul><li>If there are no items (i = 0) or the weight limit is zero (w = 0), the value is 0.</li><li>This is handled by dp[i][w] = 0 for these cases.</li></ul><p> <strong>Filling the Table (Core Logic):</strong></p><ul><li>Iterate through all items (i) and weight capacities (w).</li><li>For each item:</li><li><strong>Case 1: The item is not included.</strong><br> Use the value from the previous row:dp[i][w] = dp[i — 1][w].</li><li><strong>Case 2: The item is included (if its weight is ≤ w).</strong><br> Add the item’s value and check the remaining capacity:<br> dp[i][w] = val[i — 1] + dp[i — 1][w — wt[i — 1]].</li><li>Take the maximum of these two cases:<br> dp[i][w] = max(dp[i — 1][w], val[i — 1] + dp[i — 1][w — wt[i — 1]]).</li></ul><p> <strong>Final Result:</strong></p><ul><li>After the table is filled, dp[n][W] contains the maximum value achievable with all n items and weight limit W.</li></ul><pre>int knapSack(int W, vector&lt;int&gt;&amp; wt, vector&lt;int&gt;&amp; val) {<br>int n = wt.size();<br>vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(W + 1));<br>// Build table dp[][] in bottom-up manner<br>for (int i = 0; i &lt;= n; i++) {<br>for (int w = 0; w &lt;= W; w++) {<br>if (i == 0 || w == 0)<br>dp[i][w] = 0;<br>else if (wt[i - 1] &lt;= w)<br>dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]],<br>dp[i - 1][w]);<br>else<br>dp[i][w] = dp[i - 1][w];<br>}<br>}<br>return dp[n][W];<br>}</pre><p><strong>Practice Task:</strong></p><p>Now I would like you to try the memoization code for this problem on your own.</p><p><strong>Why DP is a Must-Learn Skill..:</strong></p><p>Dynamic Programming isn’t just another technique — it’s a game-changer. Whether you’re tackling classic problems like Fibonacci or Knapsack Problem or even more complex challenges like Longest Common Subsequence, or Matrix Chain Multiplication, DP helps you break them down, optimize them, and conquer them with style.</p><p>So the next time you’re stuck in an endless loop of calculations, let DP swoop in and save the day!</p><p>I hope you find this article helpful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=525bf74ecf26" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>