-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
time-to-cross-a-bridge.cpp
40 lines (39 loc) · 1.71 KB
/
time-to-cross-a-bridge.cpp
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
38
39
40
// Time: O(k + nlogk)
// Space: O(k)
// heap, simulation
class Solution {
public:
int findCrossingTime(int n, int k, vector<vector<int>>& time) {
vector<pair<int, int>> workers;
for (int i = 0; i < k; ++i) {
workers.emplace_back(time[i][0] + time[i][2], i);
}
priority_queue<pair<int, int>> left_bridge(cbegin(workers), cend(workers)), right_bridge;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> left_ware, right_ware;
int curr = 0;
while (n) {
while (!empty(left_ware) && left_ware.top().first <= curr) {
const auto [_, i] = left_ware.top(); left_ware.pop();
left_bridge.emplace(time[i][0] + time[i][2], i);
}
while (!empty(right_ware) && right_ware.top().first <= curr) {
const auto [_, i] = right_ware.top(); right_ware.pop();
right_bridge.emplace(time[i][0] + time[i][2], i);
}
if (!empty(right_bridge)) {
const auto [_, i] = right_bridge.top(); right_bridge.pop();
curr += time[i][2];
left_ware.emplace(curr + time[i][3], i);
--n;
} else if (!empty(left_bridge) && n - size(right_ware)) {
const auto [_, i] = left_bridge.top(); left_bridge.pop();
curr += time[i][0];
right_ware.emplace(curr + time[i][1], i);
} else {
curr = min(!empty(left_ware) ? left_ware.top().first : numeric_limits<int>::max(),
!empty(right_ware) ? right_ware.top().first : numeric_limits<int>::max());
}
}
return curr;
}
};