LeetCode #3733 — MEDIUM

Minimum Time to Complete All Deliveries

Move from brute-force thinking to an efficient approach using math strategy.

Solve on LeetCode
The Problem

Problem Statement

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.

Example 1:

Input: d = [3,1], r = [2,3]

Output: 5

Explanation:

  • The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4).
  • The second drone delivers at hour 2 (recharges at hour 3).

Example 2:

Input: d = [1,3], r = [2,2]

Output: 7

Explanation:

  • The first drone delivers at hour 3 (recharges at hours 2, 4, 6).
  • The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6).

Example 3:

Input: d = [2,1], r = [3,4]

Output: 3

Explanation:

  • The first drone delivers at hours 1, 2 (recharges at hour 3).
  • The second drone delivers at hour 3.

Constraints:

  • d = [d1, d2]
  • 1 <= di <= 109
  • r = [r1, r2]
  • 2 <= ri <= 3 * 104
Patterns Used

Roadmap

  1. Brute Force Baseline
  2. Core Insight
  3. Algorithm Walkthrough
  4. Edge Cases
  5. Full Annotated Code
  6. Interactive Study Demo
  7. Complexity Analysis
Step 01

Brute Force Baseline

Problem summary: 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.

Baseline thinking

Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.

Pattern signal: Math · Binary Search

Example 1

[3,1]
[2,3]

Example 2

[1,3]
[2,2]

Example 3

[2,1]
[3,4]
Step 02

Core Insight

What unlocks the optimal approach

  • Use binary search on the total time <code>T</code>.
  • At hours divisible by <code>lcm(r1, r2)</code>, both drones are recharging (unavailable).
  • For a fixed <code>T</code>, recharge counts are <code>floor(T / r1)</code> and <code>floor(T / r2)</code>.
  • Available hours: <code>c1 = T - floor(T / r1)</code>, <code>c2 = T - floor(T / r2)</code>; shared hours = <code>T - floor(T / r1) - floor(T / r2) + floor(T / lcm(r1,r2))</code>.
  • Assign each drone its exclusive/available hours first; remaining deliveries must fit into the <code>shared</code> hours.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03

Algorithm Walkthrough

Iteration Checklist

  1. Define state (indices, window, stack, map, DP cell, or recursion frame).
  2. Apply one transition step and update the invariant.
  3. Record answer candidate when condition is met.
  4. Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04

Edge Cases

Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05

Full Annotated Code

Source-backed implementations are provided below for direct study and interview prep.

// Accepted solution for LeetCode #3733: Minimum Time to Complete All Deliveries
// Auto-generated Java example from go.
class Solution {
    public void exampleSolution() {
    }
}
// Reference (go):
// // Accepted solution for LeetCode #3733: Minimum Time to Complete All Deliveries
// package main
// 
// import "sort"
// 
// // https://space.bilibili.com/206214
// func minimumTime1(d, r []int) int64 {
// 	d1, d2 := d[0], d[1]
// 	r1, r2 := r[0], r[1]
// 	l := lcm(r1, r2)
// 
// 	// 库函数是左闭右开区间
// 	left := d1 + d2
// 	right := (d1+d2)*2 - 1
// 	ans := left + sort.Search(right-left, func(t int) bool {
// 		t += left
// 		return d1 <= t-t/r1 && d2 <= t-t/r2 && d1+d2 <= t-t/l
// 	})
// 	return int64(ans)
// }
// 
// func f(d, r int) int {
// 	return (d-1)/(r-1) + 1
// }
// 
// func minimumTime(d, r []int) int64 {
// 	d1, d2 := d[0], d[1]
// 	r1, r2 := r[0], r[1]
// 	l := lcm(r1, r2)
// 	return int64(max(f(d1, r1), f(d2, r2), f(d1+d2, l)))
// }
// 
// func gcd(a, b int) int {
// 	for a != 0 {
// 		a, b = b%a, a
// 	}
// 	return b
// }
// 
// func lcm(a, b int) int {
// 	return a / gcd(a, b) * b
// }
Step 06

Interactive Study Demo

Use this to step through a reusable interview workflow for this problem.

Press Step or Run All to begin.
Step 07

Complexity Analysis

Time
O(log n)
Space
O(1)

Approach Breakdown

LINEAR SCAN
O(n) time
O(1) space

Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.

BINARY SEARCH
O(log n) time
O(1) space

Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).

Shortcut: Halving the input each step → O(log n). Works on any monotonic condition, not just sorted arrays.
Coach Notes

Common Mistakes

Review these before coding to avoid predictable interview regressions.

Overflow in intermediate arithmetic

Wrong move: Temporary multiplications exceed integer bounds.

Usually fails on: Large inputs wrap around unexpectedly.

Fix: Use wider types, modular arithmetic, or rearranged operations.

Boundary update without `+1` / `-1`

Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.

Usually fails on: Two-element ranges never converge.

Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.