Min Stack Design
📖 Scenario: Imagine you are building a special stack for a calculator app. This stack not only stores numbers but also quickly tells you the smallest number currently in the stack. This helps the app show the minimum value instantly without searching every time.
🎯 Goal: Build a MinStack in C that supports push, pop, top, and getMin operations all in constant time.
📋 What You'll Learn
Create a struct called
MinStack with two integer arrays: stack and minStack, and an integer top for the current index.Add a variable
maxSize to set the maximum size of the stack.Implement
push to add a number to stack and update minStack with the current minimum.Implement
pop to remove the top element from both stack and minStack.Implement
top to return the top element of stack.Implement
getMin to return the current minimum from minStack.Print the results of
top and getMin after operations.💡 Why This Matters
🌍 Real World
Min stacks are used in calculators, stock price trackers, and algorithms that need quick access to minimum values.
💼 Career
Understanding min stack design helps in coding interviews and building efficient data structures for real-time applications.
Progress0 / 4 steps
