Java O(NlogN)
Complexity\n- Time complexity: O(NlogN)\n\n# Code\n\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<Order> p
# Complexity\n- Time complexity: O(NlogN)\n\n# Code\n```\nclass Solution {\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        PriorityQueue<Order> pqb = new PriorityQueue<>((a, b) -> b.price - a.price);\n        PriorityQueue<Order> pqs = new PriorityQueue<>((a, b) -> a.price - b.price);\n        \n        for (int[] o: orders) {\n            Order curr = new Order(o[0], o[1]);\n            \n            if (o[2] == 0) {\n                //buy\n                while (!pqs.isEmpty()) {\n                    Order s = pqs.poll();\n                    if (curr.price >= s.price) {\n                        int diff = curr.amount - s.amount;\n                        if (diff > 0) {\n                            curr.amount = diff;\n                        } else {\n                            s.amount -= curr.amount;\n                            pqs.offer(s);\n                            curr.amount = 0;\n                            break;\n                        }\n                    } else {\n                        pqs.offer(s);\n                        break;\n                    }\n                }\n                if (curr.amount > 0) pqb.offer(curr);\n            } else {\n                //sell\n                while (!pqb.isEmpty()) {\n                    Order b = pqb.poll();\n                    if (b.price >= curr.price) {\n                        int diff = curr.amount - b.amount;\n                        if (diff > 0) {\n                            curr.amount = diff;\n                        } else {\n                            b.amount -= curr.amount;\n                            pqb.offer(b);\n                            curr.amount = 0;\n                            break;\n                        }\n                    } else {\n                        pqb.offer(b);\n                        break;\n                    }\n                }\n                if (curr.amount > 0) pqs.offer(curr);\n            }\n        }\n\n        long sum = 0L;\n        while (!pqs.isEmpty()) {\n            sum += pqs.poll().amount;\n        }\n\n        while (!pqb.isEmpty()) {\n            sum += pqb.poll().amount;\n        }\n\n        return (int)(sum % 1_000_000_007L);\n    }\n}\n\nclass Order {\n    int price, amount;\n\n    public Order(int p, int a) {\n        price = p;\n        amount = a;\n    }\n}\n```
['Java']