The Example to Practice DRY
\n\nclass Order {\npublic:\n    int price;\n    int amount;\n};\n\nenum COMP { GREATER, LESS };\n\ntemplate <COMP op>\nclass OrderComp {\npublic:\n    bool oper
\n```\nclass Order {\npublic:\n    int price;\n    int amount;\n};\n\nenum COMP { GREATER, LESS };\n\ntemplate <COMP op>\nclass OrderComp {\npublic:\n    bool operator() (const Order& lhs, const Order& rhs) {\n        if (op == GREATER) {\n            return lhs.price > rhs.price;\n        }\n        return lhs.price < rhs.price;\n    }\n};\n\n\nclass Solution {\nprivate:\n    template<typename T1, typename T2>\n    void processOrder(T1& q1, T2& q2, COMP op, int price, int amount, string n1="q1", string n2="q2") {\n        if (q2.size() == 0) {\n            q1.push(Order{price, amount});\n            return;\n        }\n        \n        while(!q2.empty() && amount > 0  ){\n            Order order = q2.top(); \n            if (op == GREATER && order.price > price ) break;\n            if (op == LESS && order.price < price) break;\n\n            q2.pop();\n            //cout << "=> deQueue("<< n2 << "): " << order.price << ", "<< order.amount << endl;\n\n            int amt = min(order.amount, amount);\n            order.amount -= amt;\n            amount -= amt;\n            if (order.amount > 0) {\n                //cout << "<= enQueue("<< n2 <<"): " << order.price << ", "<< order.amount << endl;\n                q2.push(order);\n            }\n        }\n        if (amount > 0) {\n            //cout << "<= enQueue("<< n1 <<"): " << price << ", "<< amount << endl;\n            q1.push(Order{price, amount});\n        }\n    }\n    \n    template<typename T>\n    void countQ(T& q, int& amount){\n        while(!q.empty()) {\n            amount = (amount + q.top().amount) % 1000000007;\n            q.pop();\n        }\n    }\npublic:\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        \n        priority_queue<Order, vector<Order>, OrderComp<GREATER>> sell;\n        priority_queue<Order, vector<Order>, OrderComp<LESS>> buy;\n        \n        for (auto& order : orders) {\n            int& price = order[0];\n            int& amount = order[1];\n            \n            if (order[2] == 0)  { //buy order\n                processOrder(buy, sell, GREATER, price, amount, "buy", "sell");\n            }else { // sell order\n                processOrder(sell, buy, LESS, price, amount, "sell", "buy");\n            }\n        }\n        \n        int amount = 0;\n        countQ(sell, amount);\n        countQ(buy, amount);\n        return amount ;\n    }\n};\n```
[]