代码拉取完成,页面将自动刷新
class Solution {
public:
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
const int MOD = 1e9+7;
priority_queue<pair<int, int>, vector<pair<int, int>>,
less<pair<int, int>>> buyOrders;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>> sellOrders;
for (auto &&order : orders)
{
int price = order[0], amount = order[1], orderType = order[2];
if (orderType == 0)
{
while (amount > 0 && !sellOrders.empty() &&
sellOrders.top().first <= price)
{
auto sellOrder = sellOrders.top();
sellOrders.pop();
int sellAmount = min(amount, sellOrder.second);
amount -= sellAmount;
sellOrder.second -= sellAmount;
if (sellOrder.second > 0)
{
sellOrders.push(sellOrder);
}
}
if (amount > 0)
{
buyOrders.emplace(price, amount);
}
}
else
{
while (amount > 0 && !buyOrders.empty() &&
buyOrders.top().first >= price)
{
auto buyOrder = buyOrders.top();
buyOrders.pop();
int buyAmount = min(amount, buyOrder.second);
amount -= buyAmount;
buyOrder.second -= buyAmount;
if (buyOrder.second > 0)
{
buyOrders.push(buyOrder);
}
}
if (amount > 0)
{
sellOrders.emplace(price, amount);
}
}
}
int total = 0;
while (!buyOrders.empty())
{
total = (total + buyOrders.top().second) % MOD;
buyOrders.pop();
}
while (!sellOrders.empty())
{
total = (total + sellOrders.top().second) % MOD;
sellOrders.pop();
}
return total;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。