{"id":"86866529-8b81-4723-965e-741d5324a46d","name":"Minimum Stack - I","description":"1. You are required to complete the code of our MinStack class. \r\n2. As data members you've two stacks available in the class - one for data values and another for minimum values. (This is cryptic - take hint from video)\r\n2. Here is the list of functions that you are supposed to complete\r\n2.1. push -> Should accept new data in LIFO manner\r\n2.2. pop -> Should remove and return data in LIFO manner. If not available, print \"Stack underflow\" and return -1.\r\n2.3. top -> Should return data in LIFO manner. If not available, print \"Stack underflow\" and return -1.\r\n2.4. size -> Should return the number of elements available in the stack\r\n2.5. min -> Should return the smallest element available in the stack. If not available, print \"Stack underflow\" and return -1.\r\n3. 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.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\nint main(int argc, char** agrc) {\r\n //write your code here\r\n\r\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> allData;\r\n Stack<Integer> minData;\r\n\r\n public MinStack() {\r\n allData = new Stack<>();\r\n minData = new Stack<>();\r\n }\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 \t // 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":"print()"}},"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/NslJ5PXAM4Q","hints":[],"associated":[{"id":"70726e44-54ed-4275-a305-4a881ff10f12","name":"What is the time complexity of this question ?(Q- Min Stack 1)","slug":"what-is-the-time-complexity-of-this-question-q-min-stack-1","type":4},{"id":"877c37c8-a92b-4bc1-b935-f52aba04cc6e","name":"From which stack will we get the “top” element ?(Q- Min Stack -1)","slug":"from-which-stack-will-we-get-the-top-element-q-min-stack-1","type":4},{"id":"bcf4dad2-31f6-4065-9a5a-f021910785dd","name":"What is the space complexity of this question ? (Min Stack -1)","slug":"what-is-the-space-complexity-of-this-question-min-stack-1","type":4},{"id":"e7a43ff1-c7ae-4833-9403-b3e6377c57a6","name":"How to get the size of the stack?(Q- Min Stack)","slug":"how-to-get-the-size-of-the-stack-q-min-stack","type":4},{"id":"ed18b70a-9f9c-410d-a958-9ca44e66fcda","name":"From which stack will we get the “min” element ?(Min Stack - 1)","slug":"from-which-stack-will-we-get-the-min-element-min-stack-1","type":4}],"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":"f15f437e-34fd-49dd-bf3f-f6ed23437b0e","name":"Minimum Stack - I","slug":"minimum-stack-i","type":1}],"next":{"id":"dd0537e8-be2d-4b0b-8e6e-2ac33d24c884","name":"Minimum Stack -1","type":3,"slug":"minimum-stack-1"},"prev":{"id":"b9959989-95fb-4c64-8524-7c5aa849ce3a","name":"Dynamic Stack","type":3,"slug":"dynamic-stack"}}}

Minimum Stack - I

1. You are required to complete the code of our MinStack class. 2. As data members you've two stacks available in the class - one for data values and another for minimum values. (This is cryptic - take hint from video) 2. Here is the list of functions that you are supposed to complete 2.1. push -> Should accept new data in LIFO manner 2.2. pop -> Should remove and return data in LIFO manner. If not available, print "Stack underflow" and return -1. 2.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 2.4. size -> Should return the number of elements available in the stack 2.5. min -> Should return the smallest element available in the stack. If not available, print "Stack underflow" and return -1. 3. 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.

{"id":"86866529-8b81-4723-965e-741d5324a46d","name":"Minimum Stack - I","description":"1. You are required to complete the code of our MinStack class. \r\n2. As data members you've two stacks available in the class - one for data values and another for minimum values. (This is cryptic - take hint from video)\r\n2. Here is the list of functions that you are supposed to complete\r\n2.1. push -> Should accept new data in LIFO manner\r\n2.2. pop -> Should remove and return data in LIFO manner. If not available, print \"Stack underflow\" and return -1.\r\n2.3. top -> Should return data in LIFO manner. If not available, print \"Stack underflow\" and return -1.\r\n2.4. size -> Should return the number of elements available in the stack\r\n2.5. min -> Should return the smallest element available in the stack. If not available, print \"Stack underflow\" and return -1.\r\n3. 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.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\nint main(int argc, char** agrc) {\r\n //write your code here\r\n\r\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> allData;\r\n Stack<Integer> minData;\r\n\r\n public MinStack() {\r\n allData = new Stack<>();\r\n minData = new Stack<>();\r\n }\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 \t // 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":"print()"}},"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/NslJ5PXAM4Q","hints":[],"associated":[{"id":"70726e44-54ed-4275-a305-4a881ff10f12","name":"What is the time complexity of this question ?(Q- Min Stack 1)","slug":"what-is-the-time-complexity-of-this-question-q-min-stack-1","type":4},{"id":"877c37c8-a92b-4bc1-b935-f52aba04cc6e","name":"From which stack will we get the “top” element ?(Q- Min Stack -1)","slug":"from-which-stack-will-we-get-the-top-element-q-min-stack-1","type":4},{"id":"bcf4dad2-31f6-4065-9a5a-f021910785dd","name":"What is the space complexity of this question ? (Min Stack -1)","slug":"what-is-the-space-complexity-of-this-question-min-stack-1","type":4},{"id":"e7a43ff1-c7ae-4833-9403-b3e6377c57a6","name":"How to get the size of the stack?(Q- Min Stack)","slug":"how-to-get-the-size-of-the-stack-q-min-stack","type":4},{"id":"ed18b70a-9f9c-410d-a958-9ca44e66fcda","name":"From which stack will we get the “min” element ?(Min Stack - 1)","slug":"from-which-stack-will-we-get-the-min-element-min-stack-1","type":4}],"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":"f15f437e-34fd-49dd-bf3f-f6ed23437b0e","name":"Minimum Stack - I","slug":"minimum-stack-i","type":1}],"next":{"id":"dd0537e8-be2d-4b0b-8e6e-2ac33d24c884","name":"Minimum Stack -1","type":3,"slug":"minimum-stack-1"},"prev":{"id":"b9959989-95fb-4c64-8524-7c5aa849ce3a","name":"Dynamic Stack","type":3,"slug":"dynamic-stack"}}}
plane

Editor


Loading...

Minimum Stack - I

easy

1. You are required to complete the code of our MinStack class. 2. As data members you've two stacks available in the class - one for data values and another for minimum values. (This is cryptic - take hint from video) 2. Here is the list of functions that you are supposed to complete 2.1. push -> Should accept new data in LIFO manner 2.2. pop -> Should remove and return data in LIFO manner. If not available, print "Stack underflow" and return -1. 2.3. top -> Should return data in LIFO manner. If not available, print "Stack underflow" and return -1. 2.4. size -> Should return the number of elements available in the stack 2.5. min -> Should return the smallest element available in the stack. If not available, print "Stack underflow" and return -1. 3. 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.

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