我遇到了这个问题,我能够解决它,但效率低下.我的解决方案是一个朴素的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)

推荐答案

您可以对orders个列表进行预处理,并在每个时间点(开始/结束)计算实际流通股数量.

然后对于每个查询使用bisect模块搜索正确的时间位置.这样你就只能做m * log n个查询.

from bisect import bisect


def calculate(orders, queries):
    outstanding_shares = []

    for a, b, c, d, e, f in orders:
        outstanding_shares.append((e, b))
        outstanding_shares.append((f, -b))

    outstanding_shares.sort()

    c = 0
    outstanding_shares = [(t, (c := c + amt)) for t, amt in outstanding_shares]

    return {
        q: outstanding_shares[bisect(outstanding_shares, (q,)) - 1][1] for q in queries
    }


# 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
]

print(calculate(orders, queries))

打印:

{500: 10, 1500: 30, 2500: 45, 3500: 50, 5500: 25}

Python相关问答推荐

如何避免Chained when/then分配中的Mypy不兼容类型警告?

将图像拖到另一个图像

无法使用requests或Selenium抓取一个href链接

Python中绕y轴曲线的旋转

numpy卷积与有效

给定高度约束的旋转角解析求解

如果满足某些条件,则用另一个数据帧列中的值填充空数据帧或数组

joblib:无法从父目录的另一个子文件夹加载转储模型

如何在FastAPI中为我上传的json文件提供索引ID?

下三角形掩码与seaborn clustermap bug

PYTHON、VLC、RTSP.屏幕截图不起作用

我对这个简单的异步者的例子有什么错误的理解吗?

在用于Python的Bokeh包中设置按钮的样式

使用Python TCP套接字发送整数并使用C#接收—接收正确数据时出错

Django抛出重复的键值违反唯一约束错误

如何将一个文件的多列导入到Python中的同一数组中?

在任何要保留的字段中添加引号的文件,就像在Pandas 中一样

大Pandas 中的群体交叉融合

删除另一个div中的特定div容器

根据边界点的属性将图划分为子图