<?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 Dhruvi Lad on Medium]]></title>
        <description><![CDATA[Stories by Dhruvi Lad on Medium]]></description>
        <link>https://medium.com/@dhruvihl369?source=rss-7d864da10028------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hEXkCObpkvAPM1Rg.jpg</url>
            <title>Stories by Dhruvi Lad on Medium</title>
            <link>https://medium.com/@dhruvihl369?source=rss-7d864da10028------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:38:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhruvihl369/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[Box Stacking Problem using Dynamic Programming]]></title>
            <link>https://medium.com/@dhruvihl369/box-stacking-problem-using-dynamic-programming-a246128dcf25?source=rss-7d864da10028------2</link>
            <guid isPermaLink="false">https://medium.com/p/a246128dcf25</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dhruvi Lad]]></dc:creator>
            <pubDate>Mon, 02 Sep 2019 16:21:18 GMT</pubDate>
            <atom:updated>2019-09-11T04:02:29.040Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Dynamic Programming:</strong></h3><p><strong>Dynamic Programming</strong> is used to optimize the solution by dividing a problem into smaller sub-problems. The idea is to store the results of sub-problems in some data structure, so that we do not have to re-process them when it is needed again. Any recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming<strong>.</strong></p><h3><strong>The Problem:</strong></h3><p>You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as possible, but you can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box.You can rotate a box so that any side functions as its base. It is also allowable to use multiple instances of the same type of box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*HWLYte_ZQh9NKWDsprzrtg.png" /></figure><p>Following are the key points to note in the problem statement:<br> 1) A box can be placed on top of another box only if both width and depth of the upper placed box are smaller than width and depth of the lower box respectively.<br> 2) We can rotate boxes such that width is smaller than depth. For example, if there is a box with dimensions {1x2x3} where 1 is height, 2×3 is base, then there can be three possibilities, {1x2x3}, {2x1x3} and {3x1x2}<br> 3) We can use multiple instances of boxes. What it means is, we can have two different rotations of a box as part of our maximum height stack.</p><h3><strong>Solution:</strong></h3><p>The box Stacking problem is a variation of <strong><em>Longest Increasing Subsequence</em></strong> Problem.</p><p>Let’s take the following boxes with their dimensions mentioned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*gEaMZ5Yq0NB_cU1aLN1nww.png" /></figure><p>We can have 3 different instance of each box by rotating it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*ejys7wmxhti3XGPyepdaDg.png" /></figure><p>Arrange the boxes un decreasing order of base area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/1*u-43qmixk01-BPaqBjhdfA.png" /></figure><p>Start form the top left all the way to the bottom right while travelling each row and keep saving the maximum height of the possible stack and corresponding box at the top.</p><p>After sorting the boxes, the problem is same as LIS with following optimal substructure property.<br> MSH(i) = Maximum possible Stack Height with box i at top of stack<br> MSH(i) = { Max ( MSH(j) ) + height(i) } where j &lt; i and width(j) &gt; width(i) and depth(j) &gt; depth(i).<br> If there is no such j then MSH(i) = height(i)</p><p>Now,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*37PX6HOIwludqPmTi43Alw.png" /></figure><p>B can not be stack on C because length(B) =lenth(C).</p><p>Same with P &amp; Q and X &amp; Y because length(R)=length(Q) and length(Z)=length(Y).</p><p>-:- A can not be stack on B because width(A)=width(B).</p><p>Same with P &amp; Q and X &amp; Y because width(P)=width(Q) and width(X)=width(Y).</p><p>-:- C can be stack on A.</p><p>Same with P &amp; Q and X &amp; Y.</p><p>-:- P can be stack on A and X can be stack on P.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*BYUwhkGT7OTNDw_mIEyL8A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a246128dcf25" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Master Theorem]]></title>
            <link>https://medium.com/@dhruvihl369/master-theorem-5f981d27d549?source=rss-7d864da10028------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f981d27d549</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dhruvi Lad]]></dc:creator>
            <pubDate>Thu, 22 Aug 2019 10:59:24 GMT</pubDate>
            <atom:updated>2019-08-22T11:20:44.585Z</atom:updated>
            <content:encoded><![CDATA[<p>The Master Theorem is used to solving the following types of recurrence equation</p><h3><strong>T(n) = a*T(n/b)+ f(n)</strong></h3><p>with a&gt;=1 and b&gt;=1 be constant &amp; f(n) be a function and (n/b) can be interpreted as</p><p>where,</p><p>n → is the size of the problem</p><p>a → is the number of sub-problems in the recursion.</p><p>n/b → is the size of each sub-problem.</p><p>f (n) → is the sum of the work done outside the recursive calls, which includes the sum of dividing the problem and the sum of combining the solution to the sub-problems.</p><p><strong>It is possible to complete an asymptotic tight bound in these three cases:</strong></p><p><strong>Case 1: </strong>If <strong>f(n)=</strong> <strong>Θ(n^c) </strong>where <strong>c&lt;log b (a) </strong>then,</p><p><strong>T(n)=</strong> <strong>Θ(nlog b (a))</strong></p><p><strong>Case 2: </strong>If <strong>f(n)=</strong> <strong>Θ(n^c log^ k( n)) </strong>where k&gt;=0 and <strong>c=log b (a) </strong>then,</p><p><strong>T(n)=</strong> <strong>Θ(n^c log ^k+1 (n))</strong></p><p><strong>Case 3</strong>: If <strong>f(n)=</strong> <strong>Θ(n^c)</strong> where <strong>c&gt;log b (a) </strong>then,</p><p><strong>T(n)=</strong> <strong>Θ(f(n))</strong></p><p><strong>Example 1:-</strong><em>T</em>(<em>n</em>)=9 <em>T</em>(n/3​)+<em>n</em></p><p>Here,</p><p>a=9,b=3,f(n)=n,c=1</p><p>log b (a)=2</p><p>so ,</p><p>c&lt;log b (a)</p><p>apply case 1 so that <strong>T(n)=</strong> <strong>Θ(n²)</strong></p><p><strong>Example 2:-</strong> <em>T</em>(<em>n</em>)=27 <em>T</em>(n/3​)+<em>n³</em></p><p>Here,</p><p>a=27,b=3,c=3,f(n)=n³</p><p>log b (a)=c</p><p>so,</p><p>c=log b (a)</p><p>apply case 2 so that<strong> T(n)=</strong> <strong>Θ(n³log n)</strong></p><p><strong>Example 3:-</strong> <em>T </em>(<em>n</em>)=2 <em>T</em>(n/2)+<em>n²</em></p><p>Here,</p><p>a=2,b=2,f(n)=n²,c=2</p><p>log b (a)=1</p><p>so,</p><p>c&gt;log b (a)</p><p>apply case 3 so that <strong>T(n)=</strong> <strong>Θ(n^2)</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f981d27d549" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to solve Knapsack problem using Greedy Method?]]></title>
            <link>https://medium.com/@dhruvihl369/how-to-solve-knapsack-problem-using-greedy-method-1b0740f5f283?source=rss-7d864da10028------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b0740f5f283</guid>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Dhruvi Lad]]></dc:creator>
            <pubDate>Thu, 22 Aug 2019 09:35:54 GMT</pubDate>
            <atom:updated>2019-08-22T09:35:54.140Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to solve Knapsack problem using Greedy Method?</h3><h3>Knapsack Problem:</h3><p>Given a set of items, each with a weight and a value, determine a subset of items to include in a collection so that total weight is less than or equal to given limit and the total value is as large as possible.</p><p>The knapsack problem is in combinatorial optimization problem.</p><h3>Optimal Solution:</h3><p>It is a feasible solution where the objective function reaches its maximum(or minimum)value-for example ,the most profit or the least cost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*feQIiHr7dt4A7e2WfBYd6w.png" /></figure><p>There are 4 way to solve this problem:</p><p>1. 0/1 knapsack problem</p><p>2. By value</p><p>3. By weight</p><p>4. Fraction method</p><p>Solution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*pVefQcSW1OvvapmOTSDXRw.png" /></figure><h3>1.0/1 Knapsack Problem:</h3><p>Step 1: Arrange values and weight in <strong>descending </strong>order according to <strong>v/w</strong>.</p><p>Step 2: After arrange values and weights in descending order take 1st object according to table and check , weight of that object is less than the limit(W). If it is less than , take that object and decrease the total weight(W) by object weight(w[]) and count the profit. If it is not less than the limit leave it and continue till N=0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*tjSUF1BATgKwqF-Ea5GStg.png" /></figure><h3>2.By Value:</h3><p>Step 1: Arrange object <strong>by value</strong> in <strong>descending </strong>order in table.</p><p>Step 2: After create a table take 1st object according to table and check , weight of that object is less than the limit(W). If it is less than , take that object and decrease the total weight(W) by object weight(w[]) and count the profit. If it is not less than the limit leave it and continue till N=0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*gAB30_daE2PFlnXCDp-uHw.png" /></figure><h3>3.By Weight:</h3><p>Step 1: Arrange object <strong>by weight</strong> in <strong>descending </strong>order in table.</p><p>Step 2: After create a table take 1st object according to table and check , weight of that object is less than the limit(W). If it is less than , take that object and decrease the total weight(W) by object weight(w[]) and count the profit. If it is not less than the limit leave it and continue till N=0</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*bcSibp5VRmVmcsPB_ML30g.png" /></figure><h3>4.Fraction Method:</h3><p>In this case, items can be broken into smaller pieces if possible.s</p><p>Step 1: Arrange values and weight in <strong>descending</strong> order according to <strong>v/w</strong>.</p><p>Step 2: After arrange values and weights in descending order take 1st object according to table and check , weight of that object is less than the limit(W). If it is less than the limit , take that object and decrease the total weight(W) by object weight(w[]) and count the profit. If it is not less than the limit fraction of that object is chosen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*yrwRcXdY0sJG1g9j2KYqOw.png" /></figure><p>Total weight=w[1]*f[1]+ w[2]*f[2]+ w[3]*f[3]+ w[4]*f[4]+ w[5]*f[5]</p><p>=(4*1)+ (1*1)+ (2*1)+ (1*1)+ (12*[7/12])</p><p>=4+1+2+1+7</p><p>=15</p><p>Total profit=v[1]*f[1]+ v[2]*f[2]+ v[3]*f[3]+ v[4]*f[4]+ v[5]*f[5]</p><p>=(10*1)+ (2*1)+ (2*1)+ (1*1)+ (4*[7/12])</p><p>=10+2+2+1+2.33</p><p>=17.33</p><h3>Reference By:</h3><h4>www.hackerearth.com</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b0740f5f283" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>