Cerinta completa
You have an empty sequence, and you will be given queries. Each query is one of these three types:
1 x -Push the element x into the stack.
2 -Delete the element present at the top of the stack.
3 -Print the maximum element in the stack.
Function Description
Complete the getMax function in the editor below.
getMax has the following parameters:
– string operations[n]: operations as strings
Returns
– int[]: the answers to each type 3 query
Input Format
The first line of input contains an integer, . The next lines each contain an above mentioned query.
Constraints
Constraints
All queries are valid.
Sample Input
STDIN Function ----- -------- 10 operations[] size n = 10 1 97 operations = ['1 97', '2', '1 20', ....] 2 1 20 2 1 26 1 20 2 3 1 91 3
Sample Output
26
91
Limbajul de programare folosit: cpp14
Cod:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) return 0;
vector<int> st, mx;
while (n--) {
int t;
cin >> t;
if (t == 1) {
int x; cin >> x;
st.push_back(x);
if (mx.empty()) mx.push_back(x);
else mx.push_back(max(mx.back(), x));
} else if (t == 2) {
st.pop_back();
mx.pop_back();
} else {
cout << mx.back() << '\n';
}
}
return 0;
}
Scor obtinut: 1.0
Submission ID: 464654793
Link challenge: https://www.hackerrank.com/challenges/maximum-element/problem
