1 Star 0 Fork 0

唐梓迅/leetcode题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
.LeetCode 1801 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
唐梓迅 提交于 2023-01-02 13:56 . 积压订单中的订单总数
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;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Tang-CMer/leetcode-problem-solving.git
git@gitee.com:Tang-CMer/leetcode-problem-solving.git
Tang-CMer
leetcode-problem-solving
leetcode题解
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385