[LeetCode] Time to Cross a Bridge

2532. Time to Cross a Bridge

There are k workers who want to move n boxes from an old warehouse to a new one. You are given the two integers n and k, and a 2D integer array time of size k x 4 where time[i] = [leftToRighti, pickOldi, rightToLefti, putNewi].

The warehouses are separated by a river and connected by a bridge. The old warehouse is on the right bank of the river, and the new warehouse is on the left bank of the river. Initially, all k workers are waiting on the left side of the bridge. To move the boxes, the ith worker (0-indexed) can :

  • Cross the bridge from the left bank (new warehouse) to the right bank (old warehouse) in leftToRighti minutes.
  • Pick a box from the old warehouse and return to the bridge in pickOldi minutes. Different workers can pick up their boxes simultaneously.
  • Cross the bridge from the right bank (old warehouse) to the left bank (new warehouse) in rightToLefti minutes.
  • Put the box in the new warehouse and return to the bridge in putNewi minutes. Different workers can put their boxes simultaneously.

A worker i is less efficient than a worker j if either condition is met:

  • leftToRighti + rightToLefti > leftToRightj + rightToLeftj
  • leftToRighti + rightToLefti == leftToRightj + rightToLeftj and i > j

The following rules regulate the movement of the workers through the bridge :

  • If a worker x reaches the bridge while another worker y is crossing the bridge, x waits at their side of the bridge.
  • If the bridge is free, the worker waiting on the right side of the bridge gets to cross the bridge. If more than one worker is waiting on the right side, the one with the lowest efficiency crosses first.
  • If the bridge is free and no worker is waiting on the right side, and at least one box remains at the old warehouse, the worker on the left side of the river gets to cross the bridge. If more than one worker is waiting on the left side, the one with the lowest efficiency crosses first.

Return the instance of time at which the last worker reaches the left bank of the river after all n boxes have been put in the new warehouse.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
int findCrossingTime(int n, int k, vector<vector<int>>& time) {
priority_queue<pair<int, int>> left, right;
priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> pick, put;
int res = 0, free = 0;
for(int i = 0; i < k; i++) left.push({time[i][0] + time[i][2], i});
while (n or put.size() or right.size()) {
if (right.empty() and (put.empty() or put.top().first > free) and (!n or left.empty() and (pick.empty() or pick.top().first > free))) {
int now = INT_MAX;
if (n and pick.size()) now = min(now, pick.top().first);
if (put.size()) now = min(now, put.top().first);
free = now;
}
while (pick.size() and pick.top().first <= free) {
auto [_, i] = pick.top(); pick.pop();
left.push({time[i][0]+time[i][2], i});
}
while (put.size() && put.top().first <= free) {
auto [_, i] = put.top(); put.pop();
right.push({time[i][0] + time[i][2], i});
}
if (right.size()) {
auto [_, i] = right.top(); right.pop();
free += time[i][2];
if (n) pick.push({free + time[i][3], i});
else res = max(res, free);
} else {
auto [_, i] = left.top(); left.pop();
free += time[i][0];
put.push({free + time[i][1], i});
--n;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/02/PS/LeetCode/time-to-cross-a-bridge/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.