zoukankan      html  css  js  c++  java
  • [LeetCode] 1801. Number of Orders in the Backlog

    You are given a 2D integer array orders, where each orders[i] = [pricei, amounti, orderTypei] denotes that amounti orders have been placed of type orderTypei at the price pricei. The orderTypei is:

    • 0 if it is a batch of buy orders, or
    • 1 if it is a batch of sell orders.

    Note that orders[i] represents a batch of amounti independent orders with the same price and order type. All orders represented by orders[i] will be placed before all orders represented by orders[i+1] for all valid i.

    There is a backlog that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:

    • If the order is a buy order, you look at the sell order with the smallest price in the backlog. If that sell order's price is smaller than or equal to the current buy order's price, they will match and be executed, and that sell order will be removed from the backlog. Else, the buy order is added to the backlog.
    • Vice versa, if the order is a sell order, you look at the buy order with the largest price in the backlog. If that buy order's price is larger than or equal to the current sell order's price, they will match and be executed, and that buy order will be removed from the backlog. Else, the sell order is added to the backlog.

    Return the total amount of orders in the backlog after placing all the orders from the input. Since this number can be large, return it modulo 109 + 7.

    Example 1:

    Input: orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
    Output: 6
    Explanation: Here is what happens with the orders:
    - 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the backlog.
    - 2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the backlog.
    - 1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the backlog, so this order is added to the backlog.
    - 4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the backlog. The 3rd order is matched with the sell order of the least price, which is 25 and this sell order is removed from the backlog. Then, there are no more sell orders in the backlog, so the 4th order is added to the backlog.
    Finally, the backlog has 5 buy orders with price 10, and 1 buy order with price 30. So the total number of orders in the backlog is 6.
    

    Example 2:

    Input: orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
    Output: 999999984
    Explanation: Here is what happens with the orders:
    - 109 orders of type sell with price 7 are placed. There are no buy orders, so the 109 orders are added to the backlog.
    - 3 orders of type buy with price 15 are placed. They are matched with the 3 sell orders with the least price which is 7, and those 3 sell orders are removed from the backlog.
    - 999999995 orders of type buy with price 5 are placed. The least price of a sell order is 7, so the 999999995 orders are added to the backlog.
    - 1 order of type sell with price 5 is placed. It is matched with the buy order of the highest price, which is 5, and that buy order is removed from the backlog.
    Finally, the backlog has (1000000000-3) sell orders with price 7, and (999999995-1) buy orders with price 5. So the total number of orders = 1999999991, which is equal to 999999984 % (109 + 7).

    Constraints:

    • 1 <= orders.length <= 105
    • orders[i].length == 3
    • 1 <= pricei, amounti <= 109
    • orderTypei is either 0 or 1.

    积压订单中的订单总数。

    给你一个二维整数数组 orders ,其中每个 orders[i] = [pricei, amounti, orderTypei] 表示有 amounti 笔类型为 orderTypei 、价格为 pricei 的订单。

    订单类型 orderTypei 可以分为两种:

    0 表示这是一批采购订单 buy
    1 表示这是一批销售订单 sell
    注意,orders[i] 表示一批共计 amounti 笔的独立订单,这些订单的价格和类型相同。对于所有有效的 i ,由 orders[i] 表示的所有订单提交时间均早于 orders[i+1] 表示的所有订单。

    存在由未执行订单组成的 积压订单 。积压订单最初是空的。提交订单时,会发生以下情况:

    如果该订单是一笔采购订单 buy ,则可以查看积压订单中价格 最低 的销售订单 sell 。如果该销售订单 sell 的价格 低于或等于 当前采购订单 buy 的价格,则匹配并执行这两笔订单,并将销售订单 sell 从积压订单中删除。否则,采购订单 buy 将会添加到积压订单中。
    反之亦然,如果该订单是一笔销售订单 sell ,则可以查看积压订单中价格 最高 的采购订单 buy 。如果该采购订单 buy 的价格 高于或等于 当前销售订单 sell 的价格,则匹配并执行这两笔订单,并将采购订单 buy 从积压订单中删除。否则,销售订单 sell 将会添加到积压订单中。
    输入所有订单后,返回积压订单中的 订单总数 。由于数字可能很大,所以需要返回对 109 + 7 取余的结果。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/number-of-orders-in-the-backlog
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路是贪心,具体做法需要用到 priority queue。这道题没有说,但是抵消订单是没有先后顺序的,也就是说如果某两张订单是可以被抵消的,其实我们无所谓先遇到哪张订单。知道这个条件的话就会好做很多。我们为采购订单 buys 创建一个最大堆,为销售订单 sells 创建一个最小堆,然后根据订单的 orderType 来区分到底是采购订单还是销售订单,把订单分类放到两个 heap 中。当两个 heap 都不为空的时候,订单数量较小的可以被抵消掉并从其对应的heap中弹出。最后由于需要MOD的关系,我们还需要遍历两个heap中的所有元素,并进行MOD处理才能输出最后的结果。

    时间O(nlogn)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int getNumberOfBacklogOrders(int[][] orders) {
     3         int MOD = (int) Math.pow(10, 9) + 7;
     4         // maxheap, biggest price at top
     5         // <price, count>
     6         PriorityQueue<int[]> buys = new PriorityQueue<>((a, b) -> (b[0] - a[0]));
     7         // minheap, smallest price at top
     8         // <price, count>
     9         PriorityQueue<int[]> sells = new PriorityQueue<>((a, b) -> (a[0] - b[0]));
    10         for (int[] order : orders) {
    11             if (order[2] == 0) {
    12                 buys.offer(order);
    13             } else {
    14                 sells.offer(order);
    15             }
    16             while (!buys.isEmpty() && !sells.isEmpty() && sells.peek()[0] <= buys.peek()[0]) {
    17                 int k = Math.min(buys.peek()[1], sells.peek()[1]);
    18                 buys.peek()[1] -= k;
    19                 sells.peek()[1] -= k;
    20                 if (buys.peek()[1] == 0) {
    21                     buys.poll();
    22                 }
    23                 if (sells.peek()[1] == 0) {
    24                     sells.poll();
    25                 }
    26             }
    27         }
    28         int res = 0;
    29         for (int[] order : buys) {
    30             res = (res + order[1]) % MOD;
    31         }
    32         for (int[] order : sells) {
    33             res = (res + order[1]) % MOD;
    34         }
    35         return res;
    36     }
    37 }

    LeetCode 题目总结

  • 相关阅读:
    test
    在linux下安装tesseract-ocr
    xpath获取同级节点
    RobotFrameWork系统关键字之断言
    redis
    mybatis
    mysql事务隔离级别
    努力努力再努力
    不同分辨率下,页面如何自适应?
    市场上有多少种分辨率?
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14566190.html
Copyright © 2011-2022 走看看