0% found this document useful (0 votes)
2 views

Maximum_Element.cpp

Uploaded by

dotienloc2005
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Maximum_Element.cpp

Uploaded by

dotienloc2005
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <bits/stdc++.

h>

using namespace std;

string ltrim(const string &);


string rtrim(const string &);

/*
* Complete the 'getMax' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts STRING_ARRAY operations as parameter.
*/

vector<int> getMax(vector<string> operations) {


stack<int> st;
vector<int> v;
for (int i=0;i<operations.size();i++) {
string s = operations[i];
if (s[0] == '1')
{
int x = stoi(s.substr(1));
if (st.size() and st.top() > x)
{
st.push(st.top());
}
else st.push(x);
}
else if (s[0] == '2') {
st.pop();
}
else if (s[0] == '3') {
v.push_back(st.top());
}
}
return v;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string n_temp;
getline(cin, n_temp);

int n = stoi(ltrim(rtrim(n_temp)));

vector<string> ops(n);

for (int i = 0; i < n; i++) {


string ops_item;
getline(cin, ops_item);

ops[i] = ops_item;
}

vector<int> res = getMax(ops);

for (size_t i = 0; i < res.size(); i++) {


fout << res[i];

if (i != res.size() - 1) {
fout << "\n";
}
}

fout << "\n";

fout.close();

return 0;
}

string ltrim(const string &str) {


string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {


string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

You might also like