新农上路
- 积分
- 99
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-11-7
- 最后登录
- 1970-1-1
|
- # coding: utf-8
- """
- [url]https://www.codenong.com/cs106206059/[/url]
- Given a stream of incoming "buy" and "sell" orders (as lists of limit price,
- quantity, and side, like ["155", "3", "buy"]),
- determine the total quantity (or number of "shares") executed.
- A "buy" order can be executed if there is a corresponding "sell" order with a
- price that is less than or equal to the price of the "buy" order.
- Similarly, a "sell" order can be executed if there is a corresponding
- "buy" order with a price that is greater than or equal to the price of
- the "sell" order. It is possible that an order does not execute
- immediately if it isn't paired to a counterparty. In that case,
- you should keep track of that order and execute it at a later
- time when a pairing order is found. You should ensure that
- orders are filled immediately at the best possible price.
- That is, an order should be executed when it is processed,
- if possible. Further, "buy" orders should execute at the
- lowest possible price and "sell" orders at the highest possible
- price at the time the order is handled.
- Note that orders can be partially executed.
-
- --- Sample Input ---
- orders = [
- ['150', '5', 'buy'], # Order A
- ['190', '1', 'sell'], # Order B
- ['200', '1', 'sell'], # Order C
- ['100', '9', 'buy'], # Order D
- ['140', '8', 'sell'], # Order E
- ['210', '4', 'buy'], # Order F
- ]
- Sample Output
- 9
- 思路:这题老实讲,不难,只是题目意思理解费劲,解题关键是用两个priorityqueue,一个用来存buy的最大,一个用来存sell的最小。
- 然后扫描,update 两个pq;
- """
- import heapq
- class Solution:
- def __init__(self):
- self.buy = []
- self.sell = []
- self.count = 0
- def put_order(self, order):
- price, quantity, side = order
- price = int(price)
- quantity = int(quantity)
- if side == 'buy':
- # max-heap
- heapq.heappush(self.buy, (-price, price, quantity))
- elif side == 'sell':
- # min-heap
- heapq.heappush(self.sell, (price, price, quantity))
-
- self.fulfill()
- def stream(self, orders):
- for order in orders:
- self.put_order(order)
- def fulfill(self):
- while self.buy and self.sell:
- b_key, b_price, b_quantity = self.buy[0]
- s_key, s_price, s_quantity = self.sell[0]
- if b_price >= s_price:
- heapq.heappop(self.buy)
- heapq.heappop(self.sell)
- if b_quantity < s_quantity:
- heapq.heappush(self.sell, (s_key, s_price, s_quantity - b_quantity))
- elif b_quantity > s_quantity:
- heapq.heappush(self.buy, (b_key, b_price, b_quantity - s_quantity))
- self.count += min(s_quantity, b_quantity)
- else:
- break
- import unittest
- class Test(unittest.TestCase):
- def test_1(self):
- sol = Solution()
- orders = [
- ['150', '5', 'buy'], # Order A
- ['190', '1', 'sell'], # Order B
- ['200', '1', 'sell'], # Order C
- ['100', '9', 'buy'], # Order D
- ['140', '8', 'sell'], # Order E
- ['210', '4', 'buy'], # Order F
- ]
- sol.stream(orders)
- print(sol.count)
- self.assertEqual(sol.count, 9)
- def test_2(self):
- sol = Solution()
- orders = [
- ['150', '5', 'buy'], # Order A
- ['190', '1', 'sell'], # Order B
- ['200', '1', 'sell'], # Order C
- ['100', '9', 'buy'], # Order D
- ['140', '8', 'sell'], # Order E
- ['210', '100', 'buy'], # Order F
- ]
- sol.stream(orders)
- print(sol.count)
- self.assertEqual(sol.count, 10)
- def test_3(self):
- sol = Solution()
- orders = [
- ['210', '100', 'buy'], # Order F
- ['190', '1', 'sell'], # Order B
- ['200', '1', 'sell'], # Order C
- ['100', '9', 'buy'], # Order D
- ['140', '8', 'sell'], # Order E
- ['150', '5', 'buy'], # Order A
- ]
- sol.stream(orders)
- print(sol.count)
- self.assertEqual(sol.count, 10)
- def test_4(self):
- sol = Solution()
- orders = [
- ("150", "10", "buy"),
- ("165", "7", "sell"),
- ("168", "3", "buy"),
- ("155", "5", "sell"),
- ("166", "8", "buy")
- ]
- sol.stream(orders)
- print(sol.count)
- self.assertEqual(sol.count, 11)
- if __name__=="__main__":
- unittest.main(verbosity=2)
复制代码 |
|