{"id":"72f95757-7cf6-4109-8b70-b6eb334ed3f6","name":"Minimum Stack - Constant Space","description":"1. You are required to complete the code of our MinStack class. \r\n2. As data members you've one stack and a min element available in the class. (This is cryptic - take hint from video)\r\n3. Here is the list of functions that you are supposed to complete\r\n 3.1. push -> Should accept new data in LIFO manner.\r\n 3.2. pop -> Should remove and return data in LIFO manner. If not available, print \r\n \"Stack underflow\" and return -1.\r\n 3.3. top -> Should return data in LIFO manner. If not available, print \"Stack \r\n underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the stack.\r\n 3.5. min -> Should return the smallest element available in the stack. If not \r\n available, print \"Stack underflow\" and return -1.\r\n4. Input and Output is managed for you.\r\n\r\nNote -> The judge maynot be able to check if all your functions are O(1) in time, but that is what the expectation is. Also, you can only use constant space.r","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you\r\n","constraints":"None","sampleCode":{"cpp":{"code":"#include<iostream>\n#include <string>\n#include <stack>\n\nusing namespace std;\n\n\nclass MinStack{\n stack<int> data;\n int minVal;\n\n\n public: \n int size() {\n // write your code here \n }\n\n public:\n void push(int val) {\n // write your code here\n }\n\n public:\n int pop() {\n // write your code here\n }\n\n public:\n int top() {\n // write your code here\n }\n \n public:\n int min(){\n // write your code here\n }\n \n};\n\n\nint main(){\n MinStack st;\n \n string str;\n cin>>str;\n \n while(str != \"quit\"){\n \n if(str==\"push\"){\n int val;\n cin>>val;\n st.push(val);\n }\n else if(str==\"pop\"){\n int val = st.pop();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"top\"){\n int val = st.top();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"size\"){\n cout<<st.size()<<endl;\n }\n else if(str== \"min\" ){\n int val = st.min();\n if(val != -1){\n cout << val << endl; \n }\n }\n \n cin>>str;\n \n }\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class MinStack {\r\n Stack<Integer> data;\r\n int min;\r\n\r\n public MinStack() {\r\n data = new Stack<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void push(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop() {\r\n // write your code here\r\n }\r\n\r\n int top() {\r\n // write your code here\r\n }\r\n\r\n int min() {\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n MinStack st = new MinStack();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"push\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if (str.startsWith(\"pop\")) {\r\n int val = st.pop();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top\")) {\r\n int val = st.top();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(st.size());\r\n } else if (str.startsWith(\"min\")) {\r\n int val = st.min();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class MinStack:\n \n def __init__(self):\n self.data = [ ];\n self.minVal = 0\n \n def size(self):\n # write your code here\n \n\n def push(self , val):\n # write your code here\n \n \n def top(self):\n # write your code here\n \n \n \n def pop(self):\n # write your code here\n \n \n def minimum(self):\n # write your code here\n\n\ndef main():\n \n \n inpStr = str(input()).strip().split(\" \")\n st = MinStack()\n \n while inpStr[0] != \"quit\":\n if inpStr[0].strip() == \"push\":\n val = inpStr[1]\n st.push(int(val))\n elif inpStr[0].strip() == \"pop\":\n val = st.pop()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"top\":\n val = st.top()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"size\":\n print(st.size())\n elif inpStr[0].strip() == \"min\":\n val = st.minimum()\n if val != -1:\n print(val)\n \n inpStr = str(input()).strip().split(\" \")\n \n \nmain()"}},"points":10,"difficulty":"easy","sampleInput":"push 10\r\npush 20\r\npush 5\r\npush 8\r\npush 2\r\npush 4\r\npush 11\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\nquit","sampleOutput":"11\r\n2\r\n11\r\n4\r\n2\r\n4\r\n2\r\n2\r\n2\r\n8\r\n5\r\n8\r\n5\r\n5\r\n5\r\n20\r\n10\r\n20\r\n10\r\n10\r\n10","questionVideo":"https://www.youtube.com/embed/BhYvZ4kVaug","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9847c2b3-e3ad-4b1c-97d1-00206b1be68d","name":"Stacks And Queues For Beginners","slug":"stacks-and-queues-for-beginners","type":0},{"id":"872d7600-46a9-4d92-9e73-4817d499ce03","name":"Minimum Stack - Constant Space","slug":"minimum-stack-constant-space","type":1}],"next":{"id":"109f846a-3b26-4894-b676-d7b54e91ab5c","name":"Minimum Stack - Constant Space","type":3,"slug":"minimum-stack-constant-space"},"prev":{"id":"dd0537e8-be2d-4b0b-8e6e-2ac33d24c884","name":"Minimum Stack -1","type":3,"slug":"minimum-stack-1"}}}

Minimum Stack - Constant Space

1. You are required to complete the code of our MinStack class. 2. As data members you've one stack and a min element available in the class. (This is cryptic - take hint from video) 3. Here is the list of functions that you are supposed to complete 3.1. push -> Should accept new data in LIFO manner. 3.2. pop -> Should remove and return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.4. size -> Should return the number of elements available in the stack. 3.5. min -> Should return the smallest element available in the stack. If not available, print "Stack underflow" and return -1. 4. Input and Output is managed for you. Note -> The judge maynot be able to check if all your functions are O(1) in time, but that is what the expectation is. Also, you can only use constant space.r

{"id":"72f95757-7cf6-4109-8b70-b6eb334ed3f6","name":"Minimum Stack - Constant Space","description":"1. You are required to complete the code of our MinStack class. \r\n2. As data members you've one stack and a min element available in the class. (This is cryptic - take hint from video)\r\n3. Here is the list of functions that you are supposed to complete\r\n 3.1. push -> Should accept new data in LIFO manner.\r\n 3.2. pop -> Should remove and return data in LIFO manner. If not available, print \r\n \"Stack underflow\" and return -1.\r\n 3.3. top -> Should return data in LIFO manner. If not available, print \"Stack \r\n underflow\" and return -1.\r\n 3.4. size -> Should return the number of elements available in the stack.\r\n 3.5. min -> Should return the smallest element available in the stack. If not \r\n available, print \"Stack underflow\" and return -1.\r\n4. Input and Output is managed for you.\r\n\r\nNote -> The judge maynot be able to check if all your functions are O(1) in time, but that is what the expectation is. Also, you can only use constant space.r","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you\r\n","constraints":"None","sampleCode":{"cpp":{"code":"#include<iostream>\n#include <string>\n#include <stack>\n\nusing namespace std;\n\n\nclass MinStack{\n stack<int> data;\n int minVal;\n\n\n public: \n int size() {\n // write your code here \n }\n\n public:\n void push(int val) {\n // write your code here\n }\n\n public:\n int pop() {\n // write your code here\n }\n\n public:\n int top() {\n // write your code here\n }\n \n public:\n int min(){\n // write your code here\n }\n \n};\n\n\nint main(){\n MinStack st;\n \n string str;\n cin>>str;\n \n while(str != \"quit\"){\n \n if(str==\"push\"){\n int val;\n cin>>val;\n st.push(val);\n }\n else if(str==\"pop\"){\n int val = st.pop();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"top\"){\n int val = st.top();\n if(val != -1){\n cout<<val<<endl;\n }\n }\n else if(str==\"size\"){\n cout<<st.size()<<endl;\n }\n else if(str== \"min\" ){\n int val = st.min();\n if(val != -1){\n cout << val << endl; \n }\n }\n \n cin>>str;\n \n }\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class MinStack {\r\n Stack<Integer> data;\r\n int min;\r\n\r\n public MinStack() {\r\n data = new Stack<>();\r\n }\r\n\r\n int size() {\r\n // write your code here\r\n }\r\n\r\n void push(int val) {\r\n // write your code here\r\n }\r\n\r\n int pop() {\r\n // write your code here\r\n }\r\n\r\n int top() {\r\n // write your code here\r\n }\r\n\r\n int min() {\r\n // write your code here\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n MinStack st = new MinStack();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"push\")) {\r\n int val = Integer.parseInt(str.split(\" \")[1]);\r\n st.push(val);\r\n } else if (str.startsWith(\"pop\")) {\r\n int val = st.pop();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"top\")) {\r\n int val = st.top();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(st.size());\r\n } else if (str.startsWith(\"min\")) {\r\n int val = st.min();\r\n if (val != -1) {\r\n System.out.println(val);\r\n }\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":"class MinStack:\n \n def __init__(self):\n self.data = [ ];\n self.minVal = 0\n \n def size(self):\n # write your code here\n \n\n def push(self , val):\n # write your code here\n \n \n def top(self):\n # write your code here\n \n \n \n def pop(self):\n # write your code here\n \n \n def minimum(self):\n # write your code here\n\n\ndef main():\n \n \n inpStr = str(input()).strip().split(\" \")\n st = MinStack()\n \n while inpStr[0] != \"quit\":\n if inpStr[0].strip() == \"push\":\n val = inpStr[1]\n st.push(int(val))\n elif inpStr[0].strip() == \"pop\":\n val = st.pop()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"top\":\n val = st.top()\n if val != -1:\n print(val)\n elif inpStr[0].strip() == \"size\":\n print(st.size())\n elif inpStr[0].strip() == \"min\":\n val = st.minimum()\n if val != -1:\n print(val)\n \n inpStr = str(input()).strip().split(\" \")\n \n \nmain()"}},"points":10,"difficulty":"easy","sampleInput":"push 10\r\npush 20\r\npush 5\r\npush 8\r\npush 2\r\npush 4\r\npush 11\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\ntop\r\nmin\r\npop\r\nquit","sampleOutput":"11\r\n2\r\n11\r\n4\r\n2\r\n4\r\n2\r\n2\r\n2\r\n8\r\n5\r\n8\r\n5\r\n5\r\n5\r\n20\r\n10\r\n20\r\n10\r\n10\r\n10","questionVideo":"https://www.youtube.com/embed/BhYvZ4kVaug","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"9847c2b3-e3ad-4b1c-97d1-00206b1be68d","name":"Stacks And Queues For Beginners","slug":"stacks-and-queues-for-beginners","type":0},{"id":"872d7600-46a9-4d92-9e73-4817d499ce03","name":"Minimum Stack - Constant Space","slug":"minimum-stack-constant-space","type":1}],"next":{"id":"109f846a-3b26-4894-b676-d7b54e91ab5c","name":"Minimum Stack - Constant Space","type":3,"slug":"minimum-stack-constant-space"},"prev":{"id":"dd0537e8-be2d-4b0b-8e6e-2ac33d24c884","name":"Minimum Stack -1","type":3,"slug":"minimum-stack-1"}}}
plane

Editor


Loading...

Minimum Stack - Constant Space

easy

1. You are required to complete the code of our MinStack class. 2. As data members you've one stack and a min element available in the class. (This is cryptic - take hint from video) 3. Here is the list of functions that you are supposed to complete 3.1. push -> Should accept new data in LIFO manner. 3.2. pop -> Should remove and return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 3.4. size -> Should return the number of elements available in the stack. 3.5. min -> Should return the smallest element available in the stack. If not available, print "Stack underflow" and return -1. 4. Input and Output is managed for you. Note -> The judge maynot be able to check if all your functions are O(1) in time, but that is what the expectation is. Also, you can only use constant space.r

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

push 10 push 20 push 5 push 8 push 2 push 4 push 11 top min pop top min pop top min pop top min pop top min pop top min pop top min pop quit

Sample Output

11 2 11 4 2 4 2 2 2 8 5 8 5 5 5 20 10 20 10 10 10

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode