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
How do you cope with rejection?
Failing students when it might cause them economic ruin
How do we explain the use of a software on a math paper?
Would it be possible to set up a franchise in the ancient world?
If you attack a Tarrasque while swallowed, what AC do you need to beat to hit it?
How to plot a surface from a system of equations?
Latin words remembered from high school 50 years ago
Identification of a badge with Russian text
Chain rule instead of product rule
Is my company merging branches wrong?
Why is Drogon so much better in battle than Rhaegal and Viserion?
Difference between consistency and satisfiability
Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario
Can you use Windows 10 "find my PC" location feature without a Microsoft login?
What should I wear to go and sign an employment contract?
How do I unravel apparent recursion in an edef statement?
Why does snapping your fingers activate the Infinity Gauntlet?
How can sister protect herself from impulse purchases with a credit card?
Very serious stuff - Salesforce bug enabled "Modify All"
Is a reptile with diamond scales possible?
Most efficient way to switch on SObjectType?
Was murdering a slave illegal in American slavery, and if so, what punishments were given for it?
Addressing an email
How come Arya Stark wasn't hurt by this in Game of Thrones Season 8 Episode 5?
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;
$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))
;
/**
* @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
$endgroup$
add a comment |
$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))
;
/**
* @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
$endgroup$
add a comment |
$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))
;
/**
* @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
$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))
;
/**
* @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
javascript programming-challenge comparative-review ecmascript-6 stack
edited 3 hours ago
thadeuszlay
asked 5 hours ago
thadeuszlaythadeuszlay
1,174616
1,174616
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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.
$endgroup$
$begingroup$
In the first solution you mean?
$endgroup$
– thadeuszlay
2 hours ago
$begingroup$
In the second solution I keep track of the minimum value with every push and pop.
$endgroup$
– thadeuszlay
1 hour ago
add a comment |
$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];
;
$endgroup$
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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.
$endgroup$
$begingroup$
In the first solution you mean?
$endgroup$
– thadeuszlay
2 hours ago
$begingroup$
In the second solution I keep track of the minimum value with every push and pop.
$endgroup$
– thadeuszlay
1 hour ago
add a comment |
$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.
$endgroup$
$begingroup$
In the first solution you mean?
$endgroup$
– thadeuszlay
2 hours ago
$begingroup$
In the second solution I keep track of the minimum value with every push and pop.
$endgroup$
– thadeuszlay
1 hour ago
add a comment |
$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.
$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.
answered 2 hours ago
konijnkonijn
27.3k456236
27.3k456236
$begingroup$
In the first solution you mean?
$endgroup$
– thadeuszlay
2 hours ago
$begingroup$
In the second solution I keep track of the minimum value with every push and pop.
$endgroup$
– thadeuszlay
1 hour ago
add a comment |
$begingroup$
In the first solution you mean?
$endgroup$
– thadeuszlay
2 hours ago
$begingroup$
In the second solution I keep track of the minimum value with every push and pop.
$endgroup$
– thadeuszlay
1 hour ago
$begingroup$
In the first solution you mean?
$endgroup$
– thadeuszlay
2 hours ago
$begingroup$
In the first solution you mean?
$endgroup$
– thadeuszlay
2 hours ago
$begingroup$
In the second solution I keep track of the minimum value with every push and pop.
$endgroup$
– thadeuszlay
1 hour ago
$begingroup$
In the second solution I keep track of the minimum value with every push and pop.
$endgroup$
– thadeuszlay
1 hour ago
add a comment |
$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];
;
$endgroup$
add a comment |
$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];
;
$endgroup$
add a comment |
$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];
;
$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];
;
answered 38 mins ago
janosjanos
99.9k13127352
99.9k13127352
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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