Skip to content

Add MinStack class with basic stack operations#2644

Open
sadabala30 wants to merge 1 commit intosuper30admin:masterfrom
sadabala30:sadabala30-patch-1
Open

Add MinStack class with basic stack operations#2644
sadabala30 wants to merge 1 commit intosuper30admin:masterfrom
sadabala30:sadabala30-patch-1

Conversation

@sadabala30
Copy link
Copy Markdown

Implement MinStack class with push, pop, top, and getMin methods.

Implement MinStack class with push, pop, top, and getMin methods.
@super30admin
Copy link
Copy Markdown
Owner

Implement Hash Set (minstack.py)

It seems there was a mix-up in the problem you were solving. The problem provided was to implement a HashSet without using built-in libraries, but your solution is for a MinStack, which is a different data structure entirely.

For the HashSet problem, you need to design a class that supports add, remove, and contains operations. The reference solution uses a double hashing technique with a 2D boolean array to efficiently store and check for keys.

To correct this:

  1. You should create a class named MyHashSet.
  2. Implement the methods add, remove, and contains as described.
  3. Consider using a hashing mechanism to map keys to indices. The reference solution uses two levels of hashing to handle collisions, but you could also use other methods like chaining with linked lists or arrays.

Here are some steps to get started:

  • Initialize an array (or list in Python) of a certain size (e.g., 1000) to serve as buckets.
  • For each bucket, you can store elements in a list (chaining) or use a nested array (double hashing).
  • For chaining: each bucket holds a list of keys. When adding, you hash the key to find the bucket and then append the key if it's not already present. For contains, you check the list in the bucket. For remove, you remove the key from the list in the bucket.
  • For double hashing: you use two hash functions to determine the exact index in a 2D array. This is more complex but has better average performance.

Since the problem constraints are manageable (keys up to 10^6 and at most 10^4 operations), chaining is also acceptable. However, double hashing can be more space-efficient.

Please review the problem statement again and implement the correct solution. If you have any questions about the HashSet implementation, feel free to ask.

VERDICT: NEEDS_IMPROVEMENT


Implement Min Stack

Your solution is correct and meets the time and space complexity requirements. Well done! However, there are a few areas for improvement in terms of code quality:

  • Use more descriptive variable names. For example, instead of a and b, use stack and min_stack to make the code more readable and maintainable.
  • You can simplify the condition in push by using if not self.b or val <= self.b[-1]: which is equivalent but more Pythonic.
  • Although the problem constraints guarantee that pop, top, and getMin are called on non-empty stacks, your pop method checks if a is not empty. This is good defensive programming, but note that the problem states these operations are always called on non-empty stacks. So it's optional, but not necessary.

Overall, your solution is solid. Keep up the good work!

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants