[C++] 2 Approaches, Max-Heap (Bids) and Min-Heap (Asks), Level 2 Quotes Processing
Complexity\n- Time complexity: O(nlogn)\n- Space complexity: O(n)\n\n# Code\n## Approach 1 (First Push to Heap, then Match)\n\nclass Solution {\npublic:\n    in
# Complexity\n- Time complexity: $$O(nlogn)$$\n- Space complexity: $$O(n)$$\n\n# Code\n## Approach 1 (First Push to Heap, then Match)\n```\nclass Solution {\npublic:\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        priority_queue<pair<int, int>> bids;\n        priority_queue<pair<int, int>> asks;\n        long backlog = 0;\n        for (const auto& order : orders) {\n            backlog += order[1];\n            if (order[2] == 0) {\n                bids.emplace(order[0], order[1]);\n            } else {\n                asks.emplace(-order[0], order[1]);\n            }\n            while (!bids.empty() && !asks.empty()) {\n                auto [bid_pri, bid_qty] = bids.top();\n                auto [ask_pri, ask_qty] = asks.top();\n                if (bid_pri >= -ask_pri) {\n                    bids.pop();\n                    asks.pop();\n                    int match = min(bid_qty, ask_qty);\n                    backlog -= (2 * match);\n                    if (bid_qty > match) {\n                        bids.emplace(bid_pri, bid_qty - match);\n                    }\n                    if (ask_qty > match) {\n                        asks.emplace(ask_pri, ask_qty - match);\n                    }\n                } else {\n                    break;\n                }\n            }\n        }\n        int mod = 1e9 + 7;\n        return backlog % mod;\n    }\n};\n```\n## Approach 2 (First Match, then Push to Heap)\n```\nclass Solution {\npublic:\n    int getNumberOfBacklogOrders(vector<vector<int>>& orders) {\n        priority_queue<pair<int, int>> bids;\n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> asks;\n        for (const auto& order : orders) {\n            if (order[2] == 0) {\n                int qty = order[1];\n                while (!asks.empty() && qty > 0 && asks.top().first <= order[0]) {\n                    auto [ask_pri, ask_qty] = asks.top();\n                    asks.pop();\n                    if (qty - ask_qty >= 0) {\n                        qty -= ask_qty;\n                    } else {\n                        asks.emplace(ask_pri, ask_qty - qty);\n                        qty = 0;\n                    }\n                }\n                if (qty > 0) {\n                    bids.emplace(order[0], qty);\n                }\n            } else {\n                int qty = order[1];\n                while (!bids.empty() && qty > 0 && bids.top().first >= order[0]) {\n                    auto [bid_pri, bid_qty] = bids.top();\n                    bids.pop();\n                    if (qty - bid_qty >= 0) {\n                        qty -= bid_qty;\n                    } else {\n                        bids.emplace(bid_pri, bid_qty - qty);\n                        qty = 0;\n                    }\n                }\n                if (qty > 0) {\n                    asks.emplace(order[0], qty);\n                }\n            }\n        }\n        int mod = 1e9 + 7;\n        long backlog = 0;\n        while (!bids.empty()) {\n            auto [_, bid_qty] = bids.top();\n            backlog = (backlog + bid_qty) % mod;\n            bids.pop();\n        }\n        while (!asks.empty()) {\n            auto [_, ask_qty] = asks.top();\n            backlog = (backlog + ask_qty) % mod;\n            asks.pop();\n        }\n        return backlog;\n    }\n};\n```
['Heap (Priority Queue)', 'Simulation', 'C++']