C++ priority queue
\n\n    int getNumberOfBacklogOrders(vector>& orders) {\n        std::priority_queue, vector\>> buy;\n        auto cmp =  {return a[0] > b[0];};\n        std::p
\n\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        std::priority_queue<vector<int>, vector<vector<int>>> buy;\n        auto cmp = [](vector<int>& a, vector<int>& b) {return a[0] > b[0];};\n        std::priority_queue<vector<int>, vector<vector<int>>, decltype(cmp)> sell(cmp);\n        int mod = 1e9 + 7;\n        for(auto& o : orders) {\n            if (o[2] == 0) { // buy option, looking for smallest in sell queue, the price has to be <= \n                \n                int price = o[0];\n                int amount = o[1];\n                \n                while(!sell.empty() && amount > 0) {\n                    auto cur = sell.top();\n                    \n                    if (cur[0] <= o[0]) {\n                        int cnt = cur[1];\n                        \n                        if (cnt >= amount) {\n                            cnt -= amount;\n                            amount = 0;\n                            sell.pop();\n                            if (cnt > 0) {\n                                sell.push({cur[0], cnt, 1});\n                            }\n                        } else {\n                            amount -= cnt;\n                            sell.pop();\n                        }\n                    } else {\n                        break;\n                    }\n                }\n                \n                if (amount > 0) {\n                    buy.push({price, amount, 0});\n                    amount = 0;\n                }\n                \n            } else { // sell, looking for larest buy, >= \n                int price = o[0];\n                int amount = o[1];\n                \n                while(!buy.empty() && amount > 0) {\n                    auto cur = buy.top();\n                    \n                    if (cur[0] >= price) {\n                        int cnt = cur[1];\n                        \n                        if (cnt >= amount) {\n                            cnt -= amount;\n                            amount = 0;\n                            buy.pop();\n                            if (cnt > 0) {\n                                buy.push({cur[0], cnt, 0});\n                            }\n                        } else {\n                            amount -= cnt;\n                            buy.pop();\n                        }\n                        \n                    } else {\n                        break;\n                    }\n                }\n                \n                if (amount > 0) {\n                    sell.push({price, amount, 1});\n                    amount = 0;\n                }\n                \n            }\n        }\n        \n        long long res = 0;\n        \n        while(!buy.empty()) {\n            auto cur = buy.top();\n            buy.pop();\n            \n            res += cur[1];\n        }\n        \n        while(!sell.empty()) {\n            auto cur = sell.top();\n            sell.pop();\n            \n            res += cur[1];\n        }\n        \n        return res % mod;\n    }
[]