class MyQueue {
public:
stack<int> stackIn;
stack<int> stackOut;
MyQueue() {
}
void push(int x) {
stackIn.push(x);
}
int pop() {
if(stackOut.empty()){
while(!stackIn.empty()){
stackOut.push(stackIn.top());
stackIn.pop();
}
}
int x = stackOut.top();
stackOut.pop();
return x;
}
int peek() {
if(stackOut.empty()){
while(!stackIn.empty()){
stackOut.push(stackIn.top());
stackIn.pop();
}
}
return stackOut.top();
}
bool empty() {
return stackIn.empty() && stackOut.empty();
}
};
class MyStack {
public:
queue<int> q;
MyStack() {
}
void push(int x) {
q.push(x);
}
int pop() {
int n = q.size() -1;
while(n--){
q.push(q.front());
q.pop();
}
int x = q.front();
q.pop();
return x;
}
int top() {
return q.back();
}
bool empty() {
return q.empty();
}
};
class Solution {
public:
bool isValid(string s) {
stack<int> st;
for(int i{};i<s.size();++i){
if(s[i] == '('){
st.push(')');
}else if(s[i] == '{'){
st.push('}');
}else if(s[i] == '['){
st.push(']');
}
else if(st.empty() || s[i] != st.top()){
return false;
}else{
st.pop();
}
}
return st.empty();
}
};
class Solution {
public:
string removeDuplicates(string s) {
stack<int> st;
for(int i{};i<s.size();++i){
if(!st.empty() && s[i] == st.top()){
st.pop();
continue;
}
st.push(s[i]);
}
string ans;
while(!st.empty()){
ans+=st.top();
st.pop();
}
reverse(ans.begin(),ans.end());
return ans;
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(int i{};i<tokens.size();++i){
if(tokens[i] == "+"
||tokens[i] == "-"
||tokens[i] == "*"
||tokens[i] == "/"){
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
if(tokens[i] == "+") st.push(num2+num1);
if(tokens[i] == "-") st.push(num2-num1);
if(tokens[i] == "*") st.push(num2*num1);
if(tokens[i] == "/") st.push(num2/num1);
}else{
st.push(stoi(tokens[i]));
}
}
return st.top();
}
};
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
priority_queue<pair<int,int>> pq;
for(int i{};i<k;++i){
pq.emplace(nums[i],i);
}
vector<int> ans{pq.top().first};
for(int i = k;i<n;++i){
pq.emplace(nums[i],i);
while(pq.top().second <= i-k){
pq.pop();
}
ans.push_back(pq.top().first);
}
return ans;
}
};
class mycomparison{
public:
bool operator()(const pair<int,int>&lhs,const pair<int,int> &rhs){
return lhs.second > rhs.second;
}
};
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> mp;
for(int i{};i<nums.size();++i){
mp[nums[i]]++;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;
for(auto &[key,value] : mp){
pri_que.emplace(key,value);
if (pri_que.size() > k) {
pri_que.pop();
}
}
vector<int> ans;
while(!pri_que.empty()){
ans.push_back(pri_que.top().first);
pri_que.pop();
}
return ans;
}
};