1801. 積壓訂單中的訂單總數(shù)

插: 前些天發(fā)現(xiàn)了一個巨牛的人工智能學習網(wǎng)站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到網(wǎng)站。
堅持不懈,越努力越幸運,大家一起學習鴨~~~

題目:

給你一個二維整數(shù)數(shù)組 orders ,其中每個 orders[i] = [pricei, amounti, orderTypei] 表示有 amounti 筆類型為 orderTypei 、價格為 pricei 的訂單。

訂單類型 orderTypei 可以分為兩種:

0 表示這是一批采購訂單 buy
1 表示這是一批銷售訂單 sell
注意,orders[i] 表示一批共計 amounti 筆的獨立訂單,這些訂單的價格和類型相同。對于所有有效的 i ,由 orders[i] 表示的所有訂單提交時間均早于 orders[i+1] 表示的所有訂單。

存在由未執(zhí)行訂單組成的 積壓訂單 。積壓訂單最初是空的。提交訂單時,會發(fā)生以下情況:

如果該訂單是一筆采購訂單 buy ,則可以查看積壓訂單中價格 最低 的銷售訂單 sell 。如果該銷售訂單 sell 的價格 低于或等于 當前采購訂單 buy 的價格,則匹配并執(zhí)行這兩筆訂單,并將銷售訂單 sell 從積壓訂單中刪除。否則,采購訂單 buy 將會添加到積壓訂單中。
反之亦然,如果該訂單是一筆銷售訂單 sell ,則可以查看積壓訂單中價格 最高 的采購訂單 buy 。如果該采購訂單 buy 的價格 高于或等于 當前銷售訂單 sell 的價格,則匹配并執(zhí)行這兩筆訂單,并將采購訂單 buy 從積壓訂單中刪除。否則,銷售訂單 sell 將會添加到積壓訂單中。
輸入所有訂單后,返回積壓訂單中的 訂單總數(shù) 。由于數(shù)字可能很大,所以需要返回對 109 + 7 取余的結果。

示例 1:


image.png

輸入:orders = [[10,5,0],[15,2,1],[25,1,1],[30,4,0]]
輸出:6
解釋:輸入訂單后會發(fā)生下述情況:

  • 提交 5 筆采購訂單,價格為 10 。沒有銷售訂單,所以這 5 筆訂單添加到積壓訂單中。
  • 提交 2 筆銷售訂單,價格為 15 。沒有采購訂單的價格大于或等于 15 ,所以這 2 筆訂單添加到積壓訂單中。
  • 提交 1 筆銷售訂單,價格為 25 。沒有采購訂單的價格大于或等于 25 ,所以這 1 筆訂單添加到積壓訂單中。
  • 提交 4 筆采購訂單,價格為 30 。前 2 筆采購訂單與價格最低(價格為 15)的 2 筆銷售訂單匹配,從積壓訂單中刪除這 2 筆銷售訂單。第 3 筆采購訂單與價格最低的 1 筆銷售訂單匹配,銷售訂單價格為 25 ,從積壓訂單中刪除這 1 筆銷售訂單。積壓訂單中不存在更多銷售訂單,所以第 4 筆采購訂單需要添加到積壓訂單中。
    最終,積壓訂單中有 5 筆價格為 10 的采購訂單,和 1 筆價格為 30 的采購訂單。所以積壓訂單中的訂單總數(shù)為 6 。
    示例 2:


    image.png

    輸入:orders = [[7,1000000000,1],[15,3,0],[5,999999995,0],[5,1,1]]
    輸出:999999984
    解釋:輸入訂單后會發(fā)生下述情況:

  • 提交 109 筆銷售訂單,價格為 7 。沒有采購訂單,所以這 109 筆訂單添加到積壓訂單中。
  • 提交 3 筆采購訂單,價格為 15 。這些采購訂單與價格最低(價格為 7 )的 3 筆銷售訂單匹配,從積壓訂單中刪除這 3 筆銷售訂單。
  • 提交 999999995 筆采購訂單,價格為 5 。銷售訂單的最低價為 7 ,所以這 999999995 筆訂單添加到積壓訂單中。
  • 提交 1 筆銷售訂單,價格為 5 。這筆銷售訂單與價格最高(價格為 5 )的 1 筆采購訂單匹配,從積壓訂單中刪除這 1 筆采購訂單。
    最終,積壓訂單中有 (1000000000-3) 筆價格為 7 的銷售訂單,和 (999999995-1) 筆價格為 5 的采購訂單。所以積壓訂單中的訂單總數(shù)為 1999999991 ,等于 999999984 % (109 + 7) 。

java代碼:

class Solution {
    public int getNumberOfBacklogOrders(int[][] orders) {
        final int MOD = 1000000007;
        PriorityQueue<int[]> buyOrders = new PriorityQueue<int[]>((a, b) -> b[0] - a[0]);
        PriorityQueue<int[]> sellOrders = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);
        for (int[] order : orders) {
            int price = order[0], amount = order[1], orderType = order[2];
            if (orderType == 0) {
                while (amount > 0 && !sellOrders.isEmpty() && sellOrders.peek()[0] <= price) {
                    int[] sellOrder = sellOrders.poll();
                    int sellAmount = Math.min(amount, sellOrder[1]);
                    amount -= sellAmount;
                    sellOrder[1] -= sellAmount;
                    if (sellOrder[1] > 0) {
                        sellOrders.offer(sellOrder);
                    }
                }
                if (amount > 0) {
                    buyOrders.offer(new int[]{price, amount});
                }
            } else {
                while (amount > 0 && !buyOrders.isEmpty() && buyOrders.peek()[0] >= price) {
                    int[] buyOrder = buyOrders.poll();
                    int buyAmount = Math.min(amount, buyOrder[1]);
                    amount -= buyAmount;
                    buyOrder[1] -= buyAmount;
                    if (buyOrder[1] > 0) {
                        buyOrders.offer(buyOrder);
                    }
                }
                if (amount > 0) {
                    sellOrders.offer(new int[]{price, amount});
                }
            }
        }
        int total = 0;
        for (PriorityQueue<int[]> pq : Arrays.asList(buyOrders, sellOrders)) {
            while (!pq.isEmpty()) {
                int[] order = pq.poll();
                total = (total + order[1]) % MOD;
            }
        }
        return total;
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容