zoukankan      html  css  js  c++  java
  • Design a stack that supports getMin() in O(1) time and O(1) extra space

    Question: Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. All these operations of SpecialStack must be O(1). To implement SpecialStack, you should only use standard Stack data structure and no other data structure like arrays, list, .. etc.

    Example:

    Consider the following SpecialStack
    16  --> TOP
    15
    29
    19
    18
    
    When getMin() is called it should return 15, 
    which is the minimum element in the current stack. 
    
    If we do pop two times on stack, the stack becomes
    29  --> TOP
    19
    18
    
    When getMin() is called, it should return 18 
    which is the minimum in the current stack.
    
    • 解答
    # Class to make a Node 
    class Node: 
    	# Constructor which assign argument to nade's value 
    	def __init__(self, value): 
    		self.value = value 
    		self.next = None
    
    	# This method returns the string representation of the object. 
    	def __str__(self): 
    		return "Node({})".format(self.value) 
    	
    	# __repr__ is same as __str__ 
    	__repr__ = __str__ 
    
    
    class Stack: 
    	# Stack Constructor initialise top of stack and counter. 
    	def __init__(self): 
    		self.top = None
    		self.count = 0
    		self.minimum = None
    		
    	# This method returns the string representation of the object (stack). 
    	def __str__(self): 
    		temp = self.top 
    		out = [] 
    		while temp: 
    			out.append(str(temp.value)) 
    			temp = temp.next
    		out = '
    '.join(out) 
    		return ('Top {} 
    
    Stack :
    {}'.format(self.top,out)) 
    		
    	# __repr__ is same as __str__ 
    	__repr__=__str__ 
    	
    	# This method is used to get minimum element of stack 
    	def getMin(self): 
    		if self.top is None: 
    			return "Stack is empty"
    		else: 
    			print("Minimum Element in the stack is: {}" .format(self.minimum)) 
    
    
    
    	# Method to check if Stack is Empty or not 
    	def isEmpty(self): 
    		# If top equals to None then stack is empty 
    		if self.top == None: 
    			return True
    		else: 
    		# If top not equal to None then stack is empty 
    			return False
    
    	# This method returns length of stack	 
    	def __len__(self): 
    		self.count = 0
    		tempNode = self.top 
    		while tempNode: 
    			tempNode = tempNode.next
    			self.count+=1
    		return self.count 
    
    	# This method returns top of stack	 
    	def peek(self): 
    		if self.top is None: 
    			print ("Stack is empty") 
    		else: 
    			if self.top.value < self.minimum: 
    				print("Top Most Element is: {}" .format(self.minimum)) 
    			else: 
    				print("Top Most Element is: {}" .format(self.top.value)) 
    
    	# This method is used to add node to stack 
    	def push(self,value): 
    		if self.top is None: 
    			self.top = Node(value) 
    			self.minimum = value 
    		
    		elif value < self.minimum: 
    			temp = (2 * value) - self.minimum 
    			new_node = Node(temp) 
    			new_node.next = self.top 
    			self.top = new_node 
    			self.minimum = value 
    		else: 
    			new_node = Node(value) 
    			new_node.next = self.top 
    			self.top = new_node 
    		print("Number Inserted: {}" .format(value)) 
    
    	# This method is used to pop top of stack 
    	def pop(self): 
    		if self.top is None: 
    			print( "Stack is empty") 
    		else: 
    			removedNode = self.top.value 
    			self.top = self.top.next
    			if removedNode < self.minimum: 
    				print ("Top Most Element Removed :{} " .format(self.minimum)) 
    				self.minimum = ( ( 2 * self.minimum ) - removedNode ) 
    			else: 
    				print ("Top Most Element Removed : {}" .format(removedNode)) 
    
    				
    			
    	
    # Driver program to test above class 
    stack = Stack() 
    
    stack.push(3) 
    stack.push(5) 
    stack.getMin() 
    stack.push(2) 
    stack.push(1) 
    stack.getMin()	 
    stack.pop() 
    stack.getMin() 
    stack.pop() 
    stack.peek() 
    
    # This code is contributed by Blinkii 
    
    • 分析

    https://www.geeksforgeeks.org/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space/

  • 相关阅读:
    阅读论文《基于神经网络的数据挖掘分类算法比较和分析研究》 安徽大学 工程硕士:常凯 (一)BP,SVM,ELM
    阅读论文《基于神经网络的数据挖掘分类算法比较和分析研究》 安徽大学 工程硕士:常凯 (二)数据集的介绍
    English Lessson-(Culture)
    【C# MVC 5】HTML Razor 的 视图model 的 属性设置
    【C# MVC 5】VS Razer 文字样式设置
    【C# SQLite】SQLite 问题集(一)
    【C++犯错记录】VS2019 MFC不懂的批量添加资源
    【C++犯错记录】VS2019 MFC添加资源不懂如何修改资源宏ID
    【数学模型】拟合平面
    【C++犯错记录】MFC项目中找不到OnInitDialog方法
  • 原文地址:https://www.cnblogs.com/HongjianChen/p/11607454.html
Copyright © 2011-2022 走看看