Create a min stackRetrieve min from stack in O(1)Stack challenge - improving memory consumptionStack with 'getMinimum' operationStack with a minimumMake a new design of Stack with a new functionSorting a Stack in ascending orderSort a stack in descending orderPushdown-stack: Test if a pop order is legal or notPython implementation of stack to return minimum in O(1) timeA Stack Template

Need to read my home electrical Meter

Manager questioning my time estimates for a project

Why do Russians almost not use verbs of possession akin to "have"?

Function argument returning void or non-void type

Why did Drogon spare this character?

Why isn't 'chemically-strengthened glass' made with potassium carbonate to begin with?

Why haven't we yet tried accelerating a space station with people inside to a near light speed?

Why did Jon Snow do this immoral act if he is so honorable?

Is it truly impossible to tell what a CPU is doing?

What does kpsewhich stand for?

Are black holes spherical during merger?

How to melt snow without fire or body heat?

Public transport tickets in UK for two weeks

Can a wizard copy a spell without first identifying it?

How did NASA Langley end up with the first 737?

Are runways booked by airlines to land their planes?

Python program to take in two strings and print the larger string

Parallel fifths in the orchestra

Why was this character made Grand Maester?

Mercedes C180 (W204) dash symbol

Must a warlock replace spells with new spells of exactly their Pact Magic spell slot level?

Can my floppy disk still work without a shutter spring?

Find this cartoon

Why does Bran want to find Drogon?



Create a min stack


Retrieve min from stack in O(1)Stack challenge - improving memory consumptionStack with 'getMinimum' operationStack with a minimumMake a new design of Stack with a new functionSorting a Stack in ascending orderSort a stack in descending orderPushdown-stack: Test if a pop order is legal or notPython implementation of stack to return minimum in O(1) timeA Stack Template






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


The task
is taken from leetcode




Design a stack that supports push, pop, top, and retrieving the
minimum element in constant time.



push(x) -- Push element x onto stack.



pop() -- Removes the element on top of the stack.



top() -- Get the top element.



getMin() -- Retrieve the minimum element in the stack.



Example:



MinStack minStack = new MinStack();



minStack.push(-2);



minStack.push(0);



minStack.push(-3);



minStack.getMin(); --> Returns -3.



minStack.pop();



minStack.top(); --> Returns 0.



minStack.getMin(); --> Returns -2.




My first solution



/**
* initialize your data structure here.
*/
var MinStack = function()
this.repo = [];
;

/**
* @param number x
* @return void
*/
MinStack.prototype.push = function(x)
if (!isNaN(x))
this.repo.push(x);

;

/**
* @return void
*/
MinStack.prototype.pop = function()
return this.repo.pop();
;

/**
* @return number
*/
MinStack.prototype.top = function()
return this.repo[this.repo.length - 1];
;

/**
* @return number
*/
MinStack.prototype.getMin = function()
if (this.repo)
const copy = this.repo.slice(0);
return copy.sort((a,b) => a - b)[0];

;


My second solution



/**
* initialize your data structure here.
*/
var MinStack = function()
this.repo = [];
this.minRepo = [];
;

/**
* @param number x
* @return void
*/
MinStack.prototype.push = function(x)
if (!isNaN(x)) x <= this.minRepo[0])
this.minRepo.unshift(x);

this.repo.push(x);

;

/**
* @return void
*/
MinStack.prototype.pop = function()
if (this.repo.pop() === this.minRepo[0])
this.minRepo.shift();

;

/**
* @return number
*/
MinStack.prototype.top = function()
return this.repo[this.repo.length - 1];
;

/**
* @return number
*/
MinStack.prototype.getMin = function()
if (this.minRepo.length)
return this.minRepo[0];

;


For the second solution I was thinking of adding the numbers not from the back (with push) but instead from the front (with unshift). The advantage is that I would need less operation inside the method top (return this.repo[0] would be sufficient - no need for calculating the last element with this.repo.length - 1). But I don't whether this would be "weird" and would mean too much "mental mapping" (the function is called push but I use a shift inside).










share|improve this question











$endgroup$


















    4












    $begingroup$


    The task
    is taken from leetcode




    Design a stack that supports push, pop, top, and retrieving the
    minimum element in constant time.



    push(x) -- Push element x onto stack.



    pop() -- Removes the element on top of the stack.



    top() -- Get the top element.



    getMin() -- Retrieve the minimum element in the stack.



    Example:



    MinStack minStack = new MinStack();



    minStack.push(-2);



    minStack.push(0);



    minStack.push(-3);



    minStack.getMin(); --> Returns -3.



    minStack.pop();



    minStack.top(); --> Returns 0.



    minStack.getMin(); --> Returns -2.




    My first solution



    /**
    * initialize your data structure here.
    */
    var MinStack = function()
    this.repo = [];
    ;

    /**
    * @param number x
    * @return void
    */
    MinStack.prototype.push = function(x)
    if (!isNaN(x))
    this.repo.push(x);

    ;

    /**
    * @return void
    */
    MinStack.prototype.pop = function()
    return this.repo.pop();
    ;

    /**
    * @return number
    */
    MinStack.prototype.top = function()
    return this.repo[this.repo.length - 1];
    ;

    /**
    * @return number
    */
    MinStack.prototype.getMin = function()
    if (this.repo)
    const copy = this.repo.slice(0);
    return copy.sort((a,b) => a - b)[0];

    ;


    My second solution



    /**
    * initialize your data structure here.
    */
    var MinStack = function()
    this.repo = [];
    this.minRepo = [];
    ;

    /**
    * @param number x
    * @return void
    */
    MinStack.prototype.push = function(x)
    if (!isNaN(x)) x <= this.minRepo[0])
    this.minRepo.unshift(x);

    this.repo.push(x);

    ;

    /**
    * @return void
    */
    MinStack.prototype.pop = function()
    if (this.repo.pop() === this.minRepo[0])
    this.minRepo.shift();

    ;

    /**
    * @return number
    */
    MinStack.prototype.top = function()
    return this.repo[this.repo.length - 1];
    ;

    /**
    * @return number
    */
    MinStack.prototype.getMin = function()
    if (this.minRepo.length)
    return this.minRepo[0];

    ;


    For the second solution I was thinking of adding the numbers not from the back (with push) but instead from the front (with unshift). The advantage is that I would need less operation inside the method top (return this.repo[0] would be sufficient - no need for calculating the last element with this.repo.length - 1). But I don't whether this would be "weird" and would mean too much "mental mapping" (the function is called push but I use a shift inside).










    share|improve this question











    $endgroup$














      4












      4








      4


      1



      $begingroup$


      The task
      is taken from leetcode




      Design a stack that supports push, pop, top, and retrieving the
      minimum element in constant time.



      push(x) -- Push element x onto stack.



      pop() -- Removes the element on top of the stack.



      top() -- Get the top element.



      getMin() -- Retrieve the minimum element in the stack.



      Example:



      MinStack minStack = new MinStack();



      minStack.push(-2);



      minStack.push(0);



      minStack.push(-3);



      minStack.getMin(); --> Returns -3.



      minStack.pop();



      minStack.top(); --> Returns 0.



      minStack.getMin(); --> Returns -2.




      My first solution



      /**
      * initialize your data structure here.
      */
      var MinStack = function()
      this.repo = [];
      ;

      /**
      * @param number x
      * @return void
      */
      MinStack.prototype.push = function(x)
      if (!isNaN(x))
      this.repo.push(x);

      ;

      /**
      * @return void
      */
      MinStack.prototype.pop = function()
      return this.repo.pop();
      ;

      /**
      * @return number
      */
      MinStack.prototype.top = function()
      return this.repo[this.repo.length - 1];
      ;

      /**
      * @return number
      */
      MinStack.prototype.getMin = function()
      if (this.repo)
      const copy = this.repo.slice(0);
      return copy.sort((a,b) => a - b)[0];

      ;


      My second solution



      /**
      * initialize your data structure here.
      */
      var MinStack = function()
      this.repo = [];
      this.minRepo = [];
      ;

      /**
      * @param number x
      * @return void
      */
      MinStack.prototype.push = function(x)
      if (!isNaN(x)) x <= this.minRepo[0])
      this.minRepo.unshift(x);

      this.repo.push(x);

      ;

      /**
      * @return void
      */
      MinStack.prototype.pop = function()
      if (this.repo.pop() === this.minRepo[0])
      this.minRepo.shift();

      ;

      /**
      * @return number
      */
      MinStack.prototype.top = function()
      return this.repo[this.repo.length - 1];
      ;

      /**
      * @return number
      */
      MinStack.prototype.getMin = function()
      if (this.minRepo.length)
      return this.minRepo[0];

      ;


      For the second solution I was thinking of adding the numbers not from the back (with push) but instead from the front (with unshift). The advantage is that I would need less operation inside the method top (return this.repo[0] would be sufficient - no need for calculating the last element with this.repo.length - 1). But I don't whether this would be "weird" and would mean too much "mental mapping" (the function is called push but I use a shift inside).










      share|improve this question











      $endgroup$




      The task
      is taken from leetcode




      Design a stack that supports push, pop, top, and retrieving the
      minimum element in constant time.



      push(x) -- Push element x onto stack.



      pop() -- Removes the element on top of the stack.



      top() -- Get the top element.



      getMin() -- Retrieve the minimum element in the stack.



      Example:



      MinStack minStack = new MinStack();



      minStack.push(-2);



      minStack.push(0);



      minStack.push(-3);



      minStack.getMin(); --> Returns -3.



      minStack.pop();



      minStack.top(); --> Returns 0.



      minStack.getMin(); --> Returns -2.




      My first solution



      /**
      * initialize your data structure here.
      */
      var MinStack = function()
      this.repo = [];
      ;

      /**
      * @param number x
      * @return void
      */
      MinStack.prototype.push = function(x)
      if (!isNaN(x))
      this.repo.push(x);

      ;

      /**
      * @return void
      */
      MinStack.prototype.pop = function()
      return this.repo.pop();
      ;

      /**
      * @return number
      */
      MinStack.prototype.top = function()
      return this.repo[this.repo.length - 1];
      ;

      /**
      * @return number
      */
      MinStack.prototype.getMin = function()
      if (this.repo)
      const copy = this.repo.slice(0);
      return copy.sort((a,b) => a - b)[0];

      ;


      My second solution



      /**
      * initialize your data structure here.
      */
      var MinStack = function()
      this.repo = [];
      this.minRepo = [];
      ;

      /**
      * @param number x
      * @return void
      */
      MinStack.prototype.push = function(x)
      if (!isNaN(x)) x <= this.minRepo[0])
      this.minRepo.unshift(x);

      this.repo.push(x);

      ;

      /**
      * @return void
      */
      MinStack.prototype.pop = function()
      if (this.repo.pop() === this.minRepo[0])
      this.minRepo.shift();

      ;

      /**
      * @return number
      */
      MinStack.prototype.top = function()
      return this.repo[this.repo.length - 1];
      ;

      /**
      * @return number
      */
      MinStack.prototype.getMin = function()
      if (this.minRepo.length)
      return this.minRepo[0];

      ;


      For the second solution I was thinking of adding the numbers not from the back (with push) but instead from the front (with unshift). The advantage is that I would need less operation inside the method top (return this.repo[0] would be sufficient - no need for calculating the last element with this.repo.length - 1). But I don't whether this would be "weird" and would mean too much "mental mapping" (the function is called push but I use a shift inside).







      javascript programming-challenge comparative-review ecmascript-6 stack






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 17 at 17:25







      thadeuszlay

















      asked May 17 at 15:20









      thadeuszlaythadeuszlay

      1,308616




      1,308616




















          2 Answers
          2






          active

          oldest

          votes


















          4












          $begingroup$

          Inconsistent style



          The getMin function checks if the stack is empty, and the top function doesn't.
          This inconsistency is confusing.
          I'm not sure which way is better, but it's good to be consistent.



          Unnecessary and over-eager input validation



          push checks if the parameter is a number, and quietly does nothing if it isn't.
          If non-numbers should not be allowed, then it would be better to throw an exception than quietly ignore.
          In any case, this check is not required by the exercise.



          Naming



          Instead of repo, it would be more natural to call it stack.
          With the push and pop methods of JavaScript arrays,
          the illusion is perfect.



          Building from common building blocks



          The second solution builds a secondary storage with the minimum values,
          and makes some effort to avoid duplicates.
          I'm not sure the extra effort is worth the added complexity.
          It would be simpler to not try to avoid duplicates,
          and simply add the pair of current and minimum values on every push.
          Then, it becomes easy to see that an implementation is possible without reimplementing a stack: under the hood you can use a stack,
          and the public methods simply encapsulate the transformations necessary for the underlying storage of value pairs.



          var MinStack = function() 
          this.stack = [];
          ;

          MinStack.prototype.push = function(x)
          const min = this.stack.length ? Math.min(this.getMin(), x) : x;
          this.stack.push([x, min]);
          ;

          MinStack.prototype.pop = function()
          this.stack.pop()[0];
          ;

          MinStack.prototype.top = function()
          return this.stack[this.stack.length - 1][0];
          ;

          MinStack.prototype.getMin = function()
          return this.stack[this.stack.length - 1][1];
          ;





          share|improve this answer









          $endgroup$












          • $begingroup$
            Your solution requires more space, doesn't it?
            $endgroup$
            – thadeuszlay
            May 18 at 6:26










          • $begingroup$
            @thadeuszlay it's on the same order of space complexity as yours ($O(n)$), but yes, on average it will use more space.
            $endgroup$
            – janos
            May 18 at 8:24


















          3












          $begingroup$

          The getMin function is not constant. You need to keep track of the minimum value whenever you push or pop.



          Furthermore you should name your functions.






          share|improve this answer









          $endgroup$












          • $begingroup$
            In the first solution you mean?
            $endgroup$
            – thadeuszlay
            May 17 at 18:30










          • $begingroup$
            In the second solution I keep track of the minimum value with every push and pop.
            $endgroup$
            – thadeuszlay
            May 17 at 19:08










          • $begingroup$
            Yes, I meant in the first anser.
            $endgroup$
            – konijn
            May 17 at 23:40











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220419%2fcreate-a-min-stack%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4












          $begingroup$

          Inconsistent style



          The getMin function checks if the stack is empty, and the top function doesn't.
          This inconsistency is confusing.
          I'm not sure which way is better, but it's good to be consistent.



          Unnecessary and over-eager input validation



          push checks if the parameter is a number, and quietly does nothing if it isn't.
          If non-numbers should not be allowed, then it would be better to throw an exception than quietly ignore.
          In any case, this check is not required by the exercise.



          Naming



          Instead of repo, it would be more natural to call it stack.
          With the push and pop methods of JavaScript arrays,
          the illusion is perfect.



          Building from common building blocks



          The second solution builds a secondary storage with the minimum values,
          and makes some effort to avoid duplicates.
          I'm not sure the extra effort is worth the added complexity.
          It would be simpler to not try to avoid duplicates,
          and simply add the pair of current and minimum values on every push.
          Then, it becomes easy to see that an implementation is possible without reimplementing a stack: under the hood you can use a stack,
          and the public methods simply encapsulate the transformations necessary for the underlying storage of value pairs.



          var MinStack = function() 
          this.stack = [];
          ;

          MinStack.prototype.push = function(x)
          const min = this.stack.length ? Math.min(this.getMin(), x) : x;
          this.stack.push([x, min]);
          ;

          MinStack.prototype.pop = function()
          this.stack.pop()[0];
          ;

          MinStack.prototype.top = function()
          return this.stack[this.stack.length - 1][0];
          ;

          MinStack.prototype.getMin = function()
          return this.stack[this.stack.length - 1][1];
          ;





          share|improve this answer









          $endgroup$












          • $begingroup$
            Your solution requires more space, doesn't it?
            $endgroup$
            – thadeuszlay
            May 18 at 6:26










          • $begingroup$
            @thadeuszlay it's on the same order of space complexity as yours ($O(n)$), but yes, on average it will use more space.
            $endgroup$
            – janos
            May 18 at 8:24















          4












          $begingroup$

          Inconsistent style



          The getMin function checks if the stack is empty, and the top function doesn't.
          This inconsistency is confusing.
          I'm not sure which way is better, but it's good to be consistent.



          Unnecessary and over-eager input validation



          push checks if the parameter is a number, and quietly does nothing if it isn't.
          If non-numbers should not be allowed, then it would be better to throw an exception than quietly ignore.
          In any case, this check is not required by the exercise.



          Naming



          Instead of repo, it would be more natural to call it stack.
          With the push and pop methods of JavaScript arrays,
          the illusion is perfect.



          Building from common building blocks



          The second solution builds a secondary storage with the minimum values,
          and makes some effort to avoid duplicates.
          I'm not sure the extra effort is worth the added complexity.
          It would be simpler to not try to avoid duplicates,
          and simply add the pair of current and minimum values on every push.
          Then, it becomes easy to see that an implementation is possible without reimplementing a stack: under the hood you can use a stack,
          and the public methods simply encapsulate the transformations necessary for the underlying storage of value pairs.



          var MinStack = function() 
          this.stack = [];
          ;

          MinStack.prototype.push = function(x)
          const min = this.stack.length ? Math.min(this.getMin(), x) : x;
          this.stack.push([x, min]);
          ;

          MinStack.prototype.pop = function()
          this.stack.pop()[0];
          ;

          MinStack.prototype.top = function()
          return this.stack[this.stack.length - 1][0];
          ;

          MinStack.prototype.getMin = function()
          return this.stack[this.stack.length - 1][1];
          ;





          share|improve this answer









          $endgroup$












          • $begingroup$
            Your solution requires more space, doesn't it?
            $endgroup$
            – thadeuszlay
            May 18 at 6:26










          • $begingroup$
            @thadeuszlay it's on the same order of space complexity as yours ($O(n)$), but yes, on average it will use more space.
            $endgroup$
            – janos
            May 18 at 8:24













          4












          4








          4





          $begingroup$

          Inconsistent style



          The getMin function checks if the stack is empty, and the top function doesn't.
          This inconsistency is confusing.
          I'm not sure which way is better, but it's good to be consistent.



          Unnecessary and over-eager input validation



          push checks if the parameter is a number, and quietly does nothing if it isn't.
          If non-numbers should not be allowed, then it would be better to throw an exception than quietly ignore.
          In any case, this check is not required by the exercise.



          Naming



          Instead of repo, it would be more natural to call it stack.
          With the push and pop methods of JavaScript arrays,
          the illusion is perfect.



          Building from common building blocks



          The second solution builds a secondary storage with the minimum values,
          and makes some effort to avoid duplicates.
          I'm not sure the extra effort is worth the added complexity.
          It would be simpler to not try to avoid duplicates,
          and simply add the pair of current and minimum values on every push.
          Then, it becomes easy to see that an implementation is possible without reimplementing a stack: under the hood you can use a stack,
          and the public methods simply encapsulate the transformations necessary for the underlying storage of value pairs.



          var MinStack = function() 
          this.stack = [];
          ;

          MinStack.prototype.push = function(x)
          const min = this.stack.length ? Math.min(this.getMin(), x) : x;
          this.stack.push([x, min]);
          ;

          MinStack.prototype.pop = function()
          this.stack.pop()[0];
          ;

          MinStack.prototype.top = function()
          return this.stack[this.stack.length - 1][0];
          ;

          MinStack.prototype.getMin = function()
          return this.stack[this.stack.length - 1][1];
          ;





          share|improve this answer









          $endgroup$



          Inconsistent style



          The getMin function checks if the stack is empty, and the top function doesn't.
          This inconsistency is confusing.
          I'm not sure which way is better, but it's good to be consistent.



          Unnecessary and over-eager input validation



          push checks if the parameter is a number, and quietly does nothing if it isn't.
          If non-numbers should not be allowed, then it would be better to throw an exception than quietly ignore.
          In any case, this check is not required by the exercise.



          Naming



          Instead of repo, it would be more natural to call it stack.
          With the push and pop methods of JavaScript arrays,
          the illusion is perfect.



          Building from common building blocks



          The second solution builds a secondary storage with the minimum values,
          and makes some effort to avoid duplicates.
          I'm not sure the extra effort is worth the added complexity.
          It would be simpler to not try to avoid duplicates,
          and simply add the pair of current and minimum values on every push.
          Then, it becomes easy to see that an implementation is possible without reimplementing a stack: under the hood you can use a stack,
          and the public methods simply encapsulate the transformations necessary for the underlying storage of value pairs.



          var MinStack = function() 
          this.stack = [];
          ;

          MinStack.prototype.push = function(x)
          const min = this.stack.length ? Math.min(this.getMin(), x) : x;
          this.stack.push([x, min]);
          ;

          MinStack.prototype.pop = function()
          this.stack.pop()[0];
          ;

          MinStack.prototype.top = function()
          return this.stack[this.stack.length - 1][0];
          ;

          MinStack.prototype.getMin = function()
          return this.stack[this.stack.length - 1][1];
          ;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 17 at 20:26









          janosjanos

          100k13127352




          100k13127352











          • $begingroup$
            Your solution requires more space, doesn't it?
            $endgroup$
            – thadeuszlay
            May 18 at 6:26










          • $begingroup$
            @thadeuszlay it's on the same order of space complexity as yours ($O(n)$), but yes, on average it will use more space.
            $endgroup$
            – janos
            May 18 at 8:24
















          • $begingroup$
            Your solution requires more space, doesn't it?
            $endgroup$
            – thadeuszlay
            May 18 at 6:26










          • $begingroup$
            @thadeuszlay it's on the same order of space complexity as yours ($O(n)$), but yes, on average it will use more space.
            $endgroup$
            – janos
            May 18 at 8:24















          $begingroup$
          Your solution requires more space, doesn't it?
          $endgroup$
          – thadeuszlay
          May 18 at 6:26




          $begingroup$
          Your solution requires more space, doesn't it?
          $endgroup$
          – thadeuszlay
          May 18 at 6:26












          $begingroup$
          @thadeuszlay it's on the same order of space complexity as yours ($O(n)$), but yes, on average it will use more space.
          $endgroup$
          – janos
          May 18 at 8:24




          $begingroup$
          @thadeuszlay it's on the same order of space complexity as yours ($O(n)$), but yes, on average it will use more space.
          $endgroup$
          – janos
          May 18 at 8:24













          3












          $begingroup$

          The getMin function is not constant. You need to keep track of the minimum value whenever you push or pop.



          Furthermore you should name your functions.






          share|improve this answer









          $endgroup$












          • $begingroup$
            In the first solution you mean?
            $endgroup$
            – thadeuszlay
            May 17 at 18:30










          • $begingroup$
            In the second solution I keep track of the minimum value with every push and pop.
            $endgroup$
            – thadeuszlay
            May 17 at 19:08










          • $begingroup$
            Yes, I meant in the first anser.
            $endgroup$
            – konijn
            May 17 at 23:40















          3












          $begingroup$

          The getMin function is not constant. You need to keep track of the minimum value whenever you push or pop.



          Furthermore you should name your functions.






          share|improve this answer









          $endgroup$












          • $begingroup$
            In the first solution you mean?
            $endgroup$
            – thadeuszlay
            May 17 at 18:30










          • $begingroup$
            In the second solution I keep track of the minimum value with every push and pop.
            $endgroup$
            – thadeuszlay
            May 17 at 19:08










          • $begingroup$
            Yes, I meant in the first anser.
            $endgroup$
            – konijn
            May 17 at 23:40













          3












          3








          3





          $begingroup$

          The getMin function is not constant. You need to keep track of the minimum value whenever you push or pop.



          Furthermore you should name your functions.






          share|improve this answer









          $endgroup$



          The getMin function is not constant. You need to keep track of the minimum value whenever you push or pop.



          Furthermore you should name your functions.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 17 at 18:27









          konijnkonijn

          27.3k456236




          27.3k456236











          • $begingroup$
            In the first solution you mean?
            $endgroup$
            – thadeuszlay
            May 17 at 18:30










          • $begingroup$
            In the second solution I keep track of the minimum value with every push and pop.
            $endgroup$
            – thadeuszlay
            May 17 at 19:08










          • $begingroup$
            Yes, I meant in the first anser.
            $endgroup$
            – konijn
            May 17 at 23:40
















          • $begingroup$
            In the first solution you mean?
            $endgroup$
            – thadeuszlay
            May 17 at 18:30










          • $begingroup$
            In the second solution I keep track of the minimum value with every push and pop.
            $endgroup$
            – thadeuszlay
            May 17 at 19:08










          • $begingroup$
            Yes, I meant in the first anser.
            $endgroup$
            – konijn
            May 17 at 23:40















          $begingroup$
          In the first solution you mean?
          $endgroup$
          – thadeuszlay
          May 17 at 18:30




          $begingroup$
          In the first solution you mean?
          $endgroup$
          – thadeuszlay
          May 17 at 18:30












          $begingroup$
          In the second solution I keep track of the minimum value with every push and pop.
          $endgroup$
          – thadeuszlay
          May 17 at 19:08




          $begingroup$
          In the second solution I keep track of the minimum value with every push and pop.
          $endgroup$
          – thadeuszlay
          May 17 at 19:08












          $begingroup$
          Yes, I meant in the first anser.
          $endgroup$
          – konijn
          May 17 at 23:40




          $begingroup$
          Yes, I meant in the first anser.
          $endgroup$
          – konijn
          May 17 at 23:40

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f220419%2fcreate-a-min-stack%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

          Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?