Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, here's what you need to do.. </p> <p>You need to maintain some <code>data structure</code> containing information of each element you insert regarding, what was the <code>minimum</code> and <code>second minimum</code> value at the time when you inserted that element.. </p> <p>So, you want to have information like this: - </p> <p><strong>For each Element pushed -></strong> </p> <ul> <li>minimum value after insertion</li> <li>minimum value before insertion</li> </ul> <p>This information will be needed when you <code>pop</code> that element from stack.. So, you would know that, whether you are <code>popping</code> minimum value or not.. If yes, then you can replace the current <code>minimum</code> value with the <code>minimum value</code> before pushing this element.. If not, then there will be no change in <code>minimum</code> value at that time.. </p> <p><strong>For E.g: -</strong> </p> <p>Suppose currently you have following element in stack: - </p> <pre><code>[3, 5, 7, 9, 2, 4] </code></pre> <ul> <li><p>And you push a <strong>value 8</strong>.. Then you have two values to maintain.. (minimum value before 8 in pushed, i.e. 2, and miminum value after 8 is inserted, i.e. 2): -</p> <pre><code>min_value_before_push = min(stack) push 8 min_value_after_push = min(stack) </code></pre></li> <li><p>And if you push a <strong>value 1</strong>, then <code>minimum_before_insertion</code> is 2, and <code>minimum_after_insertion</code> is 1: -</p> <pre><code>min_value_before_push = 2 min_value_after_push = 1 </code></pre></li> </ul> <p>Now your stack is :- </p> <pre><code>[1, 8, 3, 5, 7, 9, 2, 4] </code></pre> <p>Now, if you <strong><code>pop</code></strong>, You will see that for <strong><code>value 1</code></strong>: - </p> <pre><code> min_value_before_push = 2 min_value_after_push = 1 </code></pre> <p>So, popping will alter the minimum value, So, you change the current minimum value with the <code>minimum_value_before_push</code> of 1.. So, again your minimum is 2..</p> <p>Your current stack becomes : - </p> <pre><code>[8, 3, 5, 7, 9, 2, 4] </code></pre> <p><strong>Now, let's check whether this algorithm works for <code>duplicate</code> element : -</strong> </p> <p>Suppose you want to push a <code>value 2</code> again..</p> <pre><code>min_value_before_push = 2 min_value_after_push = 2 </code></pre> <p>Then you go on and <code>pop</code>, you see that for <code>value 2</code>, <code>min_value_after_push</code> is 2, so this means popping it will alter the minimum value.. So you replace this value with <code>min_value_before_push</code>, which is also 2.. Which is what we wanted..</p> <p><strong>NOTE</strong>: - One Benefit of this Algorithm is that, you won't need to do much comparison.. Just a comparison with <code>current_minimum_value</code> while pushing.. And a comparison with <code>current_minimum_value</code> when you <code>pop</code>..</p> <p>You can try to proceed to think what <code>data structure</code> can you have..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload