We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Stacks
- Maximum Element
- Discussions
Maximum Element
Maximum Element
+ 0 comments #include <stdio.h> #include <stdlib.h> struct stack { int *arr; int size; int top; }; int isEmpty(struct stack *s) { if(s->top == -1) return 1; return 0; } int isFull(struct stack *s) { if(s->top == s->size - 1) return 1; return 0; } void push(struct stack *s, int data) { if(!isFull(s)) { s->top++; s->arr[s->top] = data; } else { printf("Stack overflow\n"); } } int pop(struct stack *s) { int ans = -1; if(isEmpty(s)) printf("Stack underflow\n"); else { ans = s->arr[s->top]; s->top--; } return ans; } int maximum(struct stack *s) { int max = s->arr[0]; for(int i = 0; i <= s->top; i++) { if(max < s->arr[i]) max = s->arr[i]; } return max; } int main() { int query; scanf("%d",&query); struct stack *s = (struct stack *)malloc(sizeof(struct stack)); s->size = query + 1; s->top = -1; s->arr = (int *)malloc(s->size * sizeof(int)); for(int i = 0; i < query; i++) { int type; scanf("%d",&type); if(type == 1) { int data; scanf("%d",&data); push(s,data); } else if(type == 2) { pop(s); } else { int max = maximum(s); printf("%d\n",max); } } }
+ 0 comments PY3
def getMax(operations): stack = [0] my = [] for i in operations: if int(i[0]) == 1: n = int(i[2:]) stack.append(max(n,stack[-1])) elif int(i[0]) == 2: if stack: stack.pop() else: my.append(stack[-1]) return my
+ 0 comments JAVA
Approach Maintain a secondary maxStack which will keep track of the last max found.
Code
public static List<Integer> getMax(List<String> operations) { List<Integer> op = new ArrayList<>(); Stack<Integer> mainStack = new Stack<>(); Stack<Integer> maxStack = new Stack<>(); for(String s : operations){ if(s.startsWith("1")){ String[] ss = s.split(" "); Integer x = Integer.valueOf(ss[1]); //System.out.println(x); mainStack.push(x); if(maxStack.isEmpty() || x > maxStack.peek()){ maxStack.push(x); }else{ maxStack.push(maxStack.peek()); } System.out.println("X "+x+" Max "+maxStack.peek()); }else if(s.equals("2")){ mainStack.pop(); maxStack.pop(); }else{ op.add(maxStack.peek()); } } return op; } }
+ 0 comments A very easy solution in c++
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; stack<int> s; stack<int> maxStack; while(n--){ int a,b; cin>>a; if(a==1){ cin>>b; } if(a==1){ s.push(b); if(maxStack.empty()){ maxStack.push(b); } else{ maxStack.push(max(maxStack.top(),b)); } } else if(a==2){ s.pop(); maxStack.pop(); } else{ cout<<maxStack.top()<<endl; } } }
+ 0 comments def getMax(operations): # Write your code here stack = list() sorted_stack = [] answer = [] for i in range(len(operations)): query = list(map(int, operations[i].split())) if query[0] == 1: stack.append(query[1]) sorted_stack.append(query[1]) sorted_stack.sort() elif query[0] == 2: sorted_stack.remove(stack.pop()) else: answer.append(sorted_stack[-1]) return answer
Load more conversations
Sort 1415 Discussions, By:
Please Login in order to post a comment