<?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 Parv Parikh on Medium]]></title>
        <description><![CDATA[Stories by Parv Parikh on Medium]]></description>
        <link>https://medium.com/@parv51199?source=rss-6566e2d80758------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5XHprQSSLiIglFtq.jpg</url>
            <title>Stories by Parv Parikh on Medium</title>
            <link>https://medium.com/@parv51199?source=rss-6566e2d80758------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:32:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@parv51199/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[Minimum Number of Platforms problem using greedy approach]]></title>
            <link>https://medium.com/@parv51199/minimum-number-of-platforms-problem-using-greedy-approach-ba67409bd1f4?source=rss-6566e2d80758------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba67409bd1f4</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[platform-problem]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[greedy-algorithms]]></category>
            <dc:creator><![CDATA[Parv Parikh]]></dc:creator>
            <pubDate>Tue, 03 Sep 2019 17:01:19 GMT</pubDate>
            <atom:updated>2019-09-03T17:01:19.925Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Greedy Algorithm Approach : A greedy algorithm, as the name suggests, always makes the choice that seems to be the best at that moment. This means that it makes a locally-optimal choice in the hope that this choice will lead to a globally-optimal solution.</blockquote><h4>Problem:</h4><h4>Arrival and departure times of all trains are given that reach a railway station. The task is to find the minimum number of platforms required for the railway station so that no train needs to wait.</h4><p>meaning that, if the two trains have same or overlapping arrival or departure time then they need to arrive at the different platforms and task is to optimize the number of platforms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*kalFKkj1ALU83az_t0I3hQ.png" /></figure><p>Let us look at one example for that,</p><p>there are 6 trains scheduled on a railway station</p><p>The Given arrival times are : <em>9:00, 9:40, 9:50, 11:00, 15:00, 18:00</em></p><p>The Given departure times are : <em>9:10, 12:00, 11:20, 11:30, 19:00, 20:00</em></p><p>Basically, we need to find out the maximum number of trains that are there on the railway station at a given time. One way is to take every interval one by one and find the number of intervals that overlap with it. Keep track of maximum number of intervals that overlap with an interval. That becomes the maximum number of platforms required.</p><p>For this one method can be used in which all the given times are sorted, arrivals and departures, and then a table is formed which has time and event type(arrival/departure).</p><blockquote>Now, every arrival time will increment the number of platforms and every departure time will decrement the number of platforms. the table is as shown below.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*KPzXnATA-eQtLDcuNrkJeg.png" /></figure><p>So from the table it can be seen that minimum number of platforms needed are 3.</p><p>the code for the same is given below:</p><p>// Program to find minimum number of platforms <br>// required on a railway station <br>#include&lt;iostream&gt; <br>#include&lt;algorithm&gt;</p><p>using namespace std;</p><p>// Returns minimum number of platforms required <br>int findPlatform(int arr[], int dep[], int n) <br>{ <br>// Sort arrival and departure arrays <br>sort(arr, arr+n); <br>sort(dep, dep+n);</p><p>// plat_needed indicates number of platforms <br>// needed at a time <br>int plat_needed = 1, result = 1; <br>int i = 1, j = 0;</p><p>// Similar to merge in merge sort to process <br>// all events in sorted order <br>while (i &lt; n &amp;&amp; j &lt; n) <br>{ <br> // If next event in sorted order is arrival, <br> // increment count of platforms needed <br> if (arr[i] &lt;= dep[j]) <br> { <br> plat_needed++; <br> i++;</p><p>// Update result if needed <br> if (plat_needed &gt; result) <br> result = plat_needed; <br> }</p><p>// Else decrement count of platforms needed <br> else<br> { <br> plat_needed — ; <br> j++; <br> } <br>}</p><p>return result; <br>}</p><p>// Driver program to test methods of graph class <br>int main() <br>{ <br> int arr[] = {900, 940, 950, 1100, 1500, 1800}; <br> int dep[] = {910, 1200, 1120, 1130, 1900, 2000}; <br> int n = 6; <br> cout &lt;&lt; “Minimum Number of Platforms Required = “<br> &lt;&lt; findPlatform(arr, dep, n); <br> return 0; <br>}</p><p>So this was the Number of platform problem using greedy approach.</p><p>I hope you enjoyed solving the problem. Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba67409bd1f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Egg Drop Problem Using Dynamic Programming]]></title>
            <link>https://medium.com/@parv51199/egg-drop-problem-using-dynamic-programming-e22f67a1a7c3?source=rss-6566e2d80758------2</link>
            <guid isPermaLink="false">https://medium.com/p/e22f67a1a7c3</guid>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[egg-drop]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Parv Parikh]]></dc:creator>
            <pubDate>Sat, 24 Aug 2019 12:43:58 GMT</pubDate>
            <atom:updated>2019-08-24T12:43:58.371Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><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></blockquote><p>Egg Drop Problem is a very good example to understand how dynamic programming helps in achieving optimum solution with a minimum cost.</p><p><strong>The Problem :</strong></p><p>Given a certain amount of floors of a building (say f number of floors) and also given certain amount of eggs (say e number of eggs) …</p><p>What is the <strong>least amount of egg drops</strong> one has to perform to find out the <strong>threshold floor</strong>? (Threshold floor is one from which the egg starts breaking and also egg breaks for all the floors above. Also, if egg dropped from any floor below the threshold floor, it won’t break.)</p><h4><strong>Constraints:</strong></h4><ol><li>An egg that survives a fall can be used again.</li><li>A broken egg must be discarded.</li><li>The effect of a fall is the same for all eggs.</li><li>If an egg breaks when dropped, then it would break if dropped from a higher floor.</li><li>If an egg survives a fall then it would survive a shorter fall.</li></ol><p>One thing is to be remembered that we are finding The <strong>least amount of egg drops needed to find threshold and not the threshold floor itself.</strong></p><p>To start to get to the solution, Lets first look at the worst case of having only one egg and f number of floors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*_L6TKaRteI7bxFQPd3ri_w.png" /><figcaption>figure 1. One Egg, Multiple Floor scenario</figcaption></figure><p>As shown in figure 1, if there are 5 floors and only 1 egg to find the threshold floor, one has to try and drop the egg from each and every floor one by one, staring from the first floor to the last one, as any of the floor can be the threshold one. and if the threshold floor is kth one and reached threshold floor the egg will break and k will be the number of trials. number of trial required for the worst case is f (number of total floors), which is that the last floor is the threshold one.</p><blockquote>1 egg, f floors → f trials</blockquote><p>Note: We cannot select randomly any floor since if, as an example we select 4th floor and drop the egg and it breaks then it is not sure that it breaks also from 3rd floor too. Hence, we cannot find the threshold because once an egg is broken, cannot be used again.</p><blockquote>e eggs, 0 floors → 0 trials</blockquote><blockquote>e eggs, 1 floor → 1 trial</blockquote><p>Now after the special cases, lets look at the problem again.</p><p>let’s say there is a function called eggDrop(e,f)</p><p>there are two possibilities for egg to drop from a floor (say k)…</p><p>(The value of k may range from 1 to f.)</p><ol><li>It Breaks</li><li>It doesn’t Break</li></ol><p>The first possibility suggests that all the floors above the floor k breaks the egg. So none of them are threshold but the ones below the floor k including floor k are possible candidates. stating that now we have e-1 number of eggs and k-1 number of floors to check. Hence, to look at the eggDrop(e-1,k-1).</p><p>On the other hand, the second suggests that that the floors below the floor k also do not break the egg and are not threshold floors and floors above the floor k are the possible candidates. stating that we still have e number of eggs and f-k (total floors -the floor k) number of floors to check. Hence, to look at eggDrop(e,f-k).</p><p>Now, From these two possibilities we have to select the maximum one since we have to select the worst case</p><blockquote>therefore, max( eggDrop(e-1,k-1) , eggDrop(e,f-k) )</blockquote><blockquote>where, loop goes on → for (k in 1:f)</blockquote><p>now from these results of 1 to f values of k, Minimum of those worsts should be selected</p><blockquote>min{max( eggDrop(e-1,k-1) , eggDrop(e,f-k) ) , k in 1:f}</blockquote><p>and since we are making a trial on this floor too, therefore 1 is added with the above solution.</p><blockquote><strong>eggDrop(e,f) = 1+min{max( eggDrop(e-1,k-1) , eggDrop(e,f-k) ) , k in 1:f}</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o0FKo3RNQkzxKUXanCDGlg.png" /><figcaption>figure 2. recursive method</figcaption></figure><p>if we use a recursive method there are repetitive function calls. Like shown in figure 2. E as eggDrop, E(1,0) and E(2,2) are processed two times. this can be solved using dynamic programming. therefore all the results will be stored in an 2-d array of Eggs vs. Floors table.</p><p>Example: lets find optimum number of trials required for a 6 floored building and 3 eggs. eggDrop(6,3) for this we have to make a table. first making table using special cases declared above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*uHDfePqziuW12LyMguWT9A.png" /><figcaption>table 1. special cases</figcaption></figure><p>now using,</p><blockquote><strong>eggDrop(e,f) = 1+min{max( eggDrop(e-1,k-1) , eggDrop(e,f-k) ) , k in 1:f}</strong></blockquote><p>if we find eggDrop(2,2) it will be …</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*CQdYsQv8mGl945h4jOY9tA.png" /></figure><p>that is … 1 + min { max(0,1) , max(1,0)}</p><p>… 1 + min {1 , 1}</p><p>… 1+1= 2</p><p>eggDrop(2,2) = 2</p><p>if same way the whole table is filled then …</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*9h9alxhOKKaFvK99B7UAcA.png" /></figure><p>we get eggDrop(3,6)= 3.</p><p>meaning that using 3 eggs and 6 floors given, we can identify the threshold floor by just 3 falls. One can also try creating tables for any number of given eggs or floors by extending the table.</p><p>That is how Dynamic Programming helps in optimizing the solution for a given problem.</p><p>Thank you !</p><p>References :</p><p><a href="https://www.geeksforgeeks.org/egg-dropping-puzzle-dp-11/">https://www.geeksforgeeks.org/egg-dropping-puzzle-dp-11/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e22f67a1a7c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>