[C++] Simply Solution - Very Easy to Understand
Intuition\nThis is a simple matching algorithm / orderbook problem, common in financial markets.\n\n# Approach\n Describe your approach to solving the problem. 
# Intuition\nThis is a simple matching algorithm / orderbook problem, common in financial markets.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\nprivate:\n    struct Order\n    {\n    private:\n        bool buy_{};\n        int price_{};\n        int initialQty_{}, remainingQty_{};\n    public:\n        Order(const vector<int>& order) :\n            buy_{ order.at(2) == 0 }, price_{ order.at(0) }, initialQty_{ order.at(1) },\n            remainingQty_{ order.at(1) }\n        { }\n\n        bool IsBuy() const { return buy_; }\n        void Fill(int quantity)\n        {\n            if (remainingQty_ - quantity < 0)\n                throw std::logic_error("Cannot fill an order for more than the remaining quantity.");\n\n            remainingQty_ -= quantity;\n        }\n        int RemainingQty() const { return remainingQty_; }\n        int FilledQty() const { return initialQty_ - remainingQty_; }\n        int Price() const { return price_; }\n        bool IsFilled() const { return remainingQty_ == 0; }\n    };\n\n    using Orders = queue<Order>;\n    using Asks = map<int, Orders, less<>>;\n    using Bids = map<int, Orders, greater<>>;\n\npublic:\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n\n        Bids bids;\n        Asks asks;\n\n        size_t bidCount{};\n        size_t askCount{};\n\n        const auto Modulo = 1\'000\'000\'000 + 7;\n        \n        for (const auto& order : orders)\n        {\n            Order orderPointer{ order };\n\n            if (orderPointer.IsBuy())\n            {\n                bids[orderPointer.Price()].push(orderPointer);\n                bidCount++;\n            }\n            else\n            {\n                asks[orderPointer.Price()].push(orderPointer);\n                askCount++;\n            }\n\n            while (!bids.empty() && !asks.empty())\n            {\n                auto& [bestBid, bidOrders] = *bids.begin();\n                auto& [bestAsk, askOrders] = *asks.begin();\n\n                if (bestBid < bestAsk)\n                    break;\n\n                auto& bid = bidOrders.front();\n                auto& ask = askOrders.front();\n\n                auto fillQuantity = min(bid.RemainingQty(), ask.RemainingQty());\n\n                bid.Fill(fillQuantity);\n                ask.Fill(fillQuantity);\n\n                if (bid.IsFilled())\n                {\n                    bidCount--;\n                    bidOrders.pop();\n                }\n\n                if (bidOrders.empty())\n                {\n                    bids.erase(bestBid);\n                }\n\n                if (ask.IsFilled())\n                {\n                    askCount--;\n                    askOrders.pop();\n                }\n\n                if (askOrders.empty())\n                {\n                    asks.erase(bestAsk);\n                }\n            }\n        }\n\n        size_t totalBids{}, totalAsks{};\n\n        for (auto& [_, orders] : bids)\n        {\n            while (!orders.empty())\n            {\n                totalBids += orders.front().RemainingQty();\n                orders.pop();\n            }\n        }\n\n        for (auto& [_, orders] : asks)\n        {\n            while (!orders.empty())\n            {\n                totalAsks += orders.front().RemainingQty();\n                orders.pop();\n            }\n        }\n\n        return (totalBids + totalAsks) % Modulo;\n    }\n};\n```
['C++']