我遇到了这个问题,我能够解决它,但效率低下.我的解决方案是一个朴素的o(m * n),其中m是查询的数量,n是订单的数量.
我们会向您提供从我们的交易中发出的订单的日志(log)(STD:VECTOR) 一天多的时间.每个订单都有以下属性:
- ORDER_TOKEN:标识订单的唯一整数
- 股票:买入或卖出的股票数量
- 价格:买入或卖出每股的价格,
- 侧面:假=卖出,真=买入
- 创建于:创建时的时间戳
- CANCELED_OR_EXECUTED_AT:订单 已取消或已执行(已填写)
在间歇时间内有订单有效 [创建日期、已取消日期或已执行日期).也就是说,在IS中创建 INCLUSIVE和CANCED_OR_EXECUTED_AT是独占的.时间戳是 用整数表示,例如从午夜开始的毫秒数.你可以 假设每个订单都被取消或全部执行.
除了 命令,你也得到了查询向量.每个查询都有一个 field:query_time,时间戳.答案是: 在所有的订单中,所有的股票都是活的. 查询时间.已发行股份会汇总而不考虑时间,例如: 开单买入10股和卖出10股合计到20股.
我想知道有没有人有更好的方法来优化我下面的解决方案,使用任何数据 struct 或算法.我相信是有的.这是一个C++问题,但为了方便起见,我用了python语言来解决
def calculate_outstanding_shares(orders, queries):
result = {}
for query in queries:
live_trades = 0
for order in orders:
if query > order[4] and query < order[5]:
live_trades += order[1]
result[query] = live_trades
return result
# Example usage
orders = [
[3, 15, 200, True, 2000, 4000],
[1,10,100,True,0,5000],
[4, 25, 250, False, 2500, 6000],
[2,20,150,False,1000,3000],
]
queries = [
500, # Before any order
1500, # Inside the duration of the first buy order
2500, # Inside the duration of both buy orders and the start of the second sell order
3500, # Inside the duration of the second sell order and after the first sell order ends
5500 # After all orders have ended
]
result = calculate_outstanding_shares(orders, queries)
print(result)