interactive-coding-challenges/stacks-queues/stack-min.ipynb

182 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement a stack with push, pop, and min methods running O(1) time.\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"\n",
"* Is this a stack of ints?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* Push/pop on empty stack\n",
"* Push/pop on non-empty stack"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"We'll use a second stack to keep track of the minimum values.\n",
"\n",
"### Min\n",
"\n",
"* If the second stack is empty, return the max int value\n",
"* Else, return the top of the stack, without popping it\n",
"\n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(1)\n",
"\n",
"### Push\n",
"\n",
"* Push the data\n",
"* If the data is less than min\n",
" * Push data to second stack\n",
"\n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(n)\n",
"\n",
"### Pop\n",
"\n",
"* Pop the data\n",
"* If the data is equal to min\n",
" * Pop the top of the second stack\n",
"* Return the data\n",
"\n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run stack.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import sys\n",
"\n",
"class MyStack(Stack):\n",
" def __init__(self, top=None):\n",
" self.min_vals = Stack()\n",
" super(MyStack, self).__init__(top)\n",
"\n",
" def min(self):\n",
" if self.min_vals.top is None:\n",
" return sys.maxint\n",
" else:\n",
" return self.min_vals.peek()\n",
"\n",
" def push(self, data):\n",
" super(MyStack, self).push(data)\n",
" if data < self.min():\n",
" self.min_vals.push(data)\n",
"\n",
" def pop(self):\n",
" data = super(MyStack, self).pop()\n",
" if data == self.min():\n",
" self.min_vals.pop()\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Push on empty stack, non-empty stack')\n",
"stack = MyStack()\n",
"stack.push(5)\n",
"print('Push:', stack.peek(), 'Min after push:', stack.min())\n",
"stack.push(1)\n",
"print('Push:', stack.peek(), 'Min after push:', stack.min())\n",
"stack.push(3)\n",
"print('Push:', stack.peek(), 'Min after push:', stack.min())\n",
"stack.push(0)\n",
"print('Push:', stack.peek(), 'Min after push:', stack.min())\n",
"print('Pop on non-empty stack')\n",
"print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n",
"print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n",
"print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n",
"print('Pop:', stack.pop(), 'Min after pop:', stack.min())\n",
"print('Stack contents:', stack.peek())\n",
"print('Pop empty stack:', stack.pop())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}