Java | Runtime and Memory 100%
\nclass Solution {\n    int MOD = 1000000007;\n    public void insert(int elem, ArrayList<Integer> backlog, int[][] orders) {\n        int left = 0, right = bac
```\nclass Solution {\n    int MOD = 1000000007;\n    public void insert(int elem, ArrayList<Integer> backlog, int[][] orders) {\n        int left = 0, right = backlog.size();\n        while (left < right) {\n            int mid = left + (right - left) / 2;\n            if (orders[elem][0] > orders[backlog.get(mid)][0])\n                left = mid+1;\n            else\n                right = mid;\n        }\n        backlog.add(left, elem);\n    }\n    \n    public void buy(int i, ArrayList<Integer> buy_backlog, ArrayList<Integer> sell_backlog, int[][] orders) {\n        while (orders[i][1] > 0 && sell_backlog.size() > 0) {\n            int min_sell = sell_backlog.get(0);\n            if (orders[min_sell][0] > orders[i][0]) break;\n        \n            int delete = Math.min(orders[min_sell][1], orders[i][1]);\n            orders[min_sell][1] -= delete;\n            orders[i][1] -= delete;\n            if (orders[min_sell][1] == 0) sell_backlog.remove(0);\n        }\n                \n        if (orders[i][1] != 0) insert(i, buy_backlog, orders);\n    }\n    \n    public void sell(int i, ArrayList<Integer> buy_backlog, ArrayList<Integer> sell_backlog, int[][] orders) {\n        while (orders[i][1] > 0 && buy_backlog.size() > 0) {\n            int n = buy_backlog.size() - 1;\n            int max_buy = buy_backlog.get(n);\n            if (orders[max_buy][0] < orders[i][0]) break;\n        \n            int delete = Math.min(orders[max_buy][1], orders[i][1]);\n            orders[max_buy][1] -= delete;\n            orders[i][1] -= delete;\n            if (orders[max_buy][1] == 0) buy_backlog.remove(n);\n        }\n        if (orders[i][1] != 0) insert(i, sell_backlog, orders);\n    }\n\n    public int getNumberOfBacklogOrders(int[][] orders) {\n        ArrayList<Integer> buy_backlog = new ArrayList<>();\n        ArrayList<Integer> sell_backlog = new ArrayList<>();\n\n        for (int i=0; i<orders.length; i++) {\n            if (orders[i][2] == 0)\n                buy(i, buy_backlog, sell_backlog, orders);\n            else\n                sell(i, buy_backlog, sell_backlog, orders);\n        }\n        \n        int ct = 0;\n        for (int i : buy_backlog) ct = (ct + orders[i][1]) % MOD;\n        for (int i : sell_backlog) ct = (ct + orders[i][1]) % MOD;\n        \n        return ct;\n    }\n}\n```
[]