Implemented Queue using Stack#2453
Implemented Queue using Stack#2453LeelaTotapally wants to merge 2 commits intosuper30admin:masterfrom
Conversation
Create Queue using Stacks (QueueUsingStack.java)Your solution is well-structured and follows the standard approach for implementing a queue using two stacks. The code is readable and includes comments explaining the approach, which is good. Strengths:
Areas for improvement:
Here is a slightly optimized version of your code: class MyQueue {
Stack<Integer> inStack = new Stack<>();
Stack<Integer> outStack = new Stack<>();
public MyQueue() {}
public void push(int x) {
inStack.push(x);
}
private void transferIfNeeded() {
if (outStack.isEmpty()) {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
public int pop() {
transferIfNeeded();
return outStack.pop();
}
public int peek() {
transferIfNeeded();
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
}Overall, your solution is correct and efficient. The minor redundancy in the VERDICT: PASS Implement Hash MapIt seems there was a mix-up in the solution you submitted. The code you provided is for implementing a queue using two stacks (LeetCode problem 232), but the problem you were asked to solve is about implementing a hash map (LeetCode problem 706). To correctly solve the hash map problem, you should design a class
Your current solution does not address any of these requirements. Please review the problem statement again and implement the VERDICT: NEEDS_IMPROVEMENT |
No description provided.