File tree Expand file tree Collapse file tree 1 file changed +19
-19
lines changed
Simulation/2532.Time-to-Cross-a-Bridge Expand file tree Collapse file tree 1 file changed +19
-19
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,19 @@ class Solution {
13
13
priority_queue<PII>rightWait;
14
14
15
15
int ret = 0 ;
16
- int count = 0 ;
17
- int taken = 0 ;
16
+ int crossed = 0 ;
17
+ int returned = 0 ;
18
18
19
- while (count < n)
19
+ while (returned < n)
20
20
{
21
+ if (crossed == n)
22
+ {
23
+ while (!leftWait.empty ())
24
+ leftWait.pop ();
25
+ while (!leftArrive.empty ())
26
+ leftArrive.pop ();
27
+ }
28
+
21
29
while (!leftArrive.empty () && leftArrive.top ().first <= nextFree)
22
30
{
23
31
auto [arriveTime, id] = leftArrive.top ();
@@ -44,25 +52,17 @@ class Solution {
44
52
auto [_, id] = rightWait.top ();
45
53
rightWait.pop ();
46
54
nextFree += time[id][2 ];
47
- leftArrive.push ({nextFree+time[id][3 ], id});
48
-
49
- count++;
55
+ leftArrive.push ({nextFree+time[id][3 ], id});
56
+ returned++;
50
57
ret = max (ret, nextFree);
51
58
}
52
- else if (!leftWait.empty ()) // L -> R
59
+ else if (!leftWait.empty () && crossed < n ) // L -> R
53
60
{
54
- if (taken == n)
55
- {
56
- while (!leftWait.empty ())
57
- leftWait.pop ();
58
- } else
59
- {
60
- auto [_, id] = leftWait.top ();
61
- leftWait.pop ();
62
- nextFree += time[id][0 ];
63
- rightArrive.push ({nextFree+time[id][1 ], id});
64
- taken++;
65
- }
61
+ auto [_, id] = leftWait.top ();
62
+ leftWait.pop ();
63
+ nextFree += time[id][0 ];
64
+ rightArrive.push ({nextFree+time[id][1 ], id});
65
+ crossed++;
66
66
}
67
67
}
68
68
You can’t perform that action at this time.
0 commit comments