{"id":"d6f6a758-2fc0-4664-8386-9931a3915f88","name":"Max-stack","description":"Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element.\r\n\r\nImplement the MaxStack class:\r\n1. MaxStack() Initializes the stack object.\r\n2. void push(int x) Pushes element x onto the stack.\r\n3. int pop() Removes the element on top of the stack and returns it.\r\n4. int top() Gets the element on the top of the stack without removing it.\r\n5. int peekMax() Retrieves the maximum element in the stack without removing it.\r\n6. int popMax() Retrieves the maximum element in the stack and removes it. If there is more than one maximum element, only remove the top-most one.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. There will be at least 1 element when pop, peek, popmax, peekmax is called.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MaxStack {\r\n\r\n public MaxStack() {\r\n\r\n }\r\n\r\n public void push(int x) {\r\n\r\n }\r\n\r\n public int pop() {\r\n return 0;\r\n }\r\n\r\n public int top() {\r\n return 0;\r\n }\r\n\r\n public int peekMax() {\r\n return 0;\r\n }\r\n\r\n public int popMax() {\r\n return 0;\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n MaxStack maxst = new MaxStack();\r\n\r\n while (read.ready()) {\r\n String []inp = read.readLine().split(\" \");\r\n\r\n switch (inp[0]) {\r\n case \"pop\":\r\n System.out.println(maxst.pop());\r\n break;\r\n case \"top\":\r\n System.out.println(maxst.top());\r\n break;\r\n case \"popMax\":\r\n System.out.println(maxst.popMax());\r\n break;\r\n case \"peekMax\":\r\n System.out.println(maxst.peekMax());\r\n break;\r\n case \"push\":\r\n maxst.push(Integer.parseInt(inp[1]));\r\n break;\r\n }\r\n\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"push 5\r\npush 1\r\npush 5\r\ntop\r\npopMax\r\ntop\r\npeekMax\r\npop\r\ntop\r\n","sampleOutput":"5\r\n5\r\n1\r\n5\r\n1\r\n5\r\n","questionVideo":"https://www.youtube.com/embed/9by6MxQOjeM","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":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"5be63b0a-b556-47e4-9820-45c0507ff71c","name":"Max-stack","slug":"max-stack","type":1}],"next":{"id":"4e136a3d-169a-4b58-810b-e9202e32bcdd","name":"Max-stack Medium MCQ","type":0,"slug":"max-stack-medium-mcq"},"prev":{"id":"924778af-5893-4bb8-97ff-96ea5ee15feb","name":"Design Circular Deque","type":3,"slug":"design-circular-deque"}}}

Max-stack

Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element. Implement the MaxStack class: 1. MaxStack() Initializes the stack object. 2. void push(int x) Pushes element x onto the stack. 3. int pop() Removes the element on top of the stack and returns it. 4. int top() Gets the element on the top of the stack without removing it. 5. int peekMax() Retrieves the maximum element in the stack without removing it. 6. int popMax() Retrieves the maximum element in the stack and removes it. If there is more than one maximum element, only remove the top-most one.

{"id":"d6f6a758-2fc0-4664-8386-9931a3915f88","name":"Max-stack","description":"Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element.\r\n\r\nImplement the MaxStack class:\r\n1. MaxStack() Initializes the stack object.\r\n2. void push(int x) Pushes element x onto the stack.\r\n3. int pop() Removes the element on top of the stack and returns it.\r\n4. int top() Gets the element on the top of the stack without removing it.\r\n5. int peekMax() Retrieves the maximum element in the stack without removing it.\r\n6. int popMax() Retrieves the maximum element in the stack and removes it. If there is more than one maximum element, only remove the top-most one.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. There will be at least 1 element when pop, peek, popmax, peekmax is called.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MaxStack {\r\n\r\n public MaxStack() {\r\n\r\n }\r\n\r\n public void push(int x) {\r\n\r\n }\r\n\r\n public int pop() {\r\n return 0;\r\n }\r\n\r\n public int top() {\r\n return 0;\r\n }\r\n\r\n public int peekMax() {\r\n return 0;\r\n }\r\n\r\n public int popMax() {\r\n return 0;\r\n }\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n MaxStack maxst = new MaxStack();\r\n\r\n while (read.ready()) {\r\n String []inp = read.readLine().split(\" \");\r\n\r\n switch (inp[0]) {\r\n case \"pop\":\r\n System.out.println(maxst.pop());\r\n break;\r\n case \"top\":\r\n System.out.println(maxst.top());\r\n break;\r\n case \"popMax\":\r\n System.out.println(maxst.popMax());\r\n break;\r\n case \"peekMax\":\r\n System.out.println(maxst.peekMax());\r\n break;\r\n case \"push\":\r\n maxst.push(Integer.parseInt(inp[1]));\r\n break;\r\n }\r\n\r\n }\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"push 5\r\npush 1\r\npush 5\r\ntop\r\npopMax\r\ntop\r\npeekMax\r\npop\r\ntop\r\n","sampleOutput":"5\r\n5\r\n1\r\n5\r\n1\r\n5\r\n","questionVideo":"https://www.youtube.com/embed/9by6MxQOjeM","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":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"5be63b0a-b556-47e4-9820-45c0507ff71c","name":"Max-stack","slug":"max-stack","type":1}],"next":{"id":"4e136a3d-169a-4b58-810b-e9202e32bcdd","name":"Max-stack Medium MCQ","type":0,"slug":"max-stack-medium-mcq"},"prev":{"id":"924778af-5893-4bb8-97ff-96ea5ee15feb","name":"Design Circular Deque","type":3,"slug":"design-circular-deque"}}}
plane

Editor


Loading...

Max-stack

medium

Design a max stack data structure that supports the stack operations and supports finding the stack's maximum element. Implement the MaxStack class: 1. MaxStack() Initializes the stack object. 2. void push(int x) Pushes element x onto the stack. 3. int pop() Removes the element on top of the stack and returns it. 4. int top() Gets the element on the top of the stack without removing it. 5. int peekMax() Retrieves the maximum element in the stack without removing it. 6. int popMax() Retrieves the maximum element in the stack and removes it. If there is more than one maximum element, only remove the top-most one.

Constraints

1. There will be at least 1 element when pop, peek, popmax, peekmax is called.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

push 5 push 1 push 5 top popMax top peekMax pop top

Sample Output

5 5 1 5 1 5

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode