This program demonstrates a special stack that supports standard push and pop operations and can also return the minimum element in constant time. The stack utilizes an auxiliary stack that tracks the minimum values efficiently.

Program Code

package main

import (
    "fmt"
    "math"
)

type MinStack struct {
    stack    []int
    minStack []int
}

func Constructor() MinStack {
    return MinStack{
        stack:    make([]int, 0),
        minStack: make([]int, 0),
    }
}

func (this *MinStack) Push(x int) {
    this.stack = append(this.stack, x)
    min := x
    if len(this.minStack) > 0 && this.minStack[len(this.minStack)-1] < x {
        min = this.minStack[len(this.minStack)-1]
    }
    this.minStack = append(this.minStack, min)
}

func (this *MinStack) Pop() {
    this.stack = this.stack[:len(this.stack)-1]
    this.minStack = this.minStack[:len(this.minStack)-1]
}

func (this *MinStack) Top() int {
    return this.stack[len(this.stack)-1]
}

func (this *MinStack) GetMin() int {
    return this.minStack[len(this.minStack)-1]
}

func main() {
    minStack := Constructor()
    minStack.Push(-2)
    minStack.Push(0)
    minStack.Push(-3)
    fmt.Println("Current Minimum:", minStack.GetMin())  // Returns -3
    minStack.Pop()
    fmt.Println("Top element:", minStack.Top())          // Returns 0
    fmt.Println("Current Minimum:", minStack.GetMin())  // Returns -2
}

Explanation

  • Structure: The MinStack structure includes two slices: one for the stack itself and another for keeping track of the minimum values.
  • Stack Operations:
    • Push operation adds an element to the main stack and updates the minimum stack. If the new element is less than the current minimum, it becomes the new minimum; otherwise, the current minimum is repeated.
    • Pop operation removes the top element from both the main stack and the minimum stack, thus maintaining the minimum tracking accurately.
    • Top returns the top element of the main stack without removing it.
    • GetMin returns the top element of the minimum stack, which is the minimum element of the main stack.

 

Running the Program

To execute this Go program:

  1. Save the code in a file named minstack.go.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the file is stored.
  4. Run the program by typing go run minstack.go.

The output will demonstrate the functionality of the special stack, showing how the minimum value can be retrieved in constant time even as elements are pushed and popped from the stack. Adjust the main function to test different inputs and observe how the stack maintains the minimum value efficiently.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)