[LeetCode] Minimum Time to Complete All Deliveries

3733. Minimum Time to Complete All Deliveries

You are given two integer arrays of size 2: d = [d1, d2] and r = [r1, r2].

Two delivery drones are tasked with completing a specific number of deliveries. Drone i must complete di deliveries.

Each delivery takes exactly one hour and only one drone can make a delivery at any given hour.

Additionally, both drones require recharging at specific intervals during which they cannot make deliveries. Drone i must recharge every ri hours (i.e. at hours that are multiples of ri).

Return an integer denoting the minimum total time (in hours) required to complete all deliveries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
long long helper(vector<int> &A, vector<int> &B, long long t, long long l) {
long long t1 = t / B[0], t2 = t / B[1], tl = t / l;
long long a = t2 - tl, b = t1 - tl, c = t - t1 - t2 + tl;
return max(0ll, A[0] - a) + max(0ll, A[1] - b) <= c;
}

public:
long long minimumTime(vector<int> &d, vector<int> &r) {
long long lcm = 1ll * r[0] / __gcd(r[0], r[1]) * r[1];
long long le = 1, ri = 2e18, res = 2e18;
while (le <= ri) {
long long m = le + (ri - le) / 2;
bool ok = helper(d, r, m, lcm);
if (ok) {
res = m;
ri = m - 1;
} else le = m + 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/21/PS/LeetCode/minimum-time-to-complete-all-deliveries/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.