You are viewing a single comment's thread. Return to all comments →
c++ vector getMax(vector operations) { vector result; stack stk; stack maxStack;
for (string op : operations) { stringstream ss(op); int command, value; ss >> command; if (command == 1) { ss >> value; stk.push(value); if (maxStack.empty() || value >= maxStack.top()) { maxStack.push(value); } } else if (command == 2) { if (!stk.empty()) { if (stk.top() == maxStack.top()) { maxStack.pop(); } stk.pop(); } } else if (command == 3) { if (!maxStack.empty()) { result.push_back(maxStack.top()); } } } return result;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
c++ vector getMax(vector operations) { vector result; stack stk; stack maxStack;
}