Skip to content

Commit

Permalink
Update 2532.Time-to-Cross-a-Bridge.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Jan 9, 2023
1 parent 4cb8335 commit 9797471
Showing 1 changed file with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ class Solution {
priority_queue<PII>rightWait;

int ret = 0;
int count = 0;
int taken = 0;
int crossed = 0;
int returned = 0;

while (count < n)
while (returned < n)
{
if (crossed == n)
{
while (!leftWait.empty())
leftWait.pop();
while (!leftArrive.empty())
leftArrive.pop();
}

while (!leftArrive.empty() && leftArrive.top().first <= nextFree)
{
auto [arriveTime, id] = leftArrive.top();
Expand All @@ -44,25 +52,17 @@ class Solution {
auto [_, id] = rightWait.top();
rightWait.pop();
nextFree += time[id][2];
leftArrive.push({nextFree+time[id][3], id});

count++;
leftArrive.push({nextFree+time[id][3], id});
returned++;
ret = max(ret, nextFree);
}
else if (!leftWait.empty()) // L -> R
else if (!leftWait.empty() && crossed < n) // L -> R
{
if (taken == n)
{
while (!leftWait.empty())
leftWait.pop();
} else
{
auto [_, id] = leftWait.top();
leftWait.pop();
nextFree += time[id][0];
rightArrive.push({nextFree+time[id][1], id});
taken++;
}
auto [_, id] = leftWait.top();
leftWait.pop();
nextFree += time[id][0];
rightArrive.push({nextFree+time[id][1], id});
crossed++;
}
}

Expand Down

0 comments on commit 9797471

Please sign in to comment.