Minimum Speed to Arrive on Time - Problem

Imagine you're planning your daily commute to work, and you must catch exactly n trains in sequence to reach your office. Each train travels a specific distance, but here's the catch: all trains can only depart at integer hours (1:00, 2:00, 3:00, etc.).

You're given:

  • A floating-point number hour representing your total time budget to reach the office
  • An integer array dist where dist[i] is the distance (in km) of the ith train ride

Key Constraint: If a train ride finishes at a non-integer time (like 1.3 hours), you must wait until the next integer hour to board the next train. However, the last train can arrive at any time within your budget.

Goal: Find the minimum positive integer speed (km/h) that allows all trains to travel fast enough for you to reach the office within the given time limit. Return -1 if it's impossible.

Example: If you have trains with distances [1, 3, 2] and hour = 6.0, at speed 1 km/h: Train 1 takes 1 hour (depart at 0:00, arrive at 1:00), Train 2 takes 3 hours (depart at 1:00, arrive at 4:00), Train 3 takes 2 hours (depart at 4:00, arrive at 6:00). Total time = 6 hours โœ“

Input & Output

example_1.py โ€” Basic case
$ Input: dist = [1,3,2], hour = 6.0
โ€บ Output: 1
๐Ÿ’ก Note: At speed 1: Train 1 takes 1 hour (0โ†’1), wait until hour 1. Train 2 takes 3 hours (1โ†’4), wait until hour 4. Train 3 takes 2 hours (4โ†’6). Total: 6 hours exactly.
example_2.py โ€” Requires higher speed
$ Input: dist = [1,3,2], hour = 2.7
โ€บ Output: 3
๐Ÿ’ก Note: At speed 3: Train 1 takes 1/3 hour, rounded up to 1 hour. Train 2 takes 1 hour. Train 3 takes 2/3 hour. Total: 1 + 1 + 0.67 = 2.67 โ‰ค 2.7
example_3.py โ€” Impossible case
$ Input: dist = [1,1,100000], hour = 2.01
โ€บ Output: -1
๐Ÿ’ก Note: We need at least 2 integer hours for the first 2 trains, leaving only 0.01 hours for the last train of distance 100000. This is impossible at any speed.

Visualization

Tap to expand
๐Ÿš‚ Train Speed OptimizationTrain 1Distance: 1kmTime: โŒˆ1/sโŒ‰Train 2Distance: 3kmTime: โŒˆ3/sโŒ‰Train 3Distance: 2kmTime: 2/sOfficeTotal Time Budget: 6.0 hoursBinary Search Process:1. Try speed = 5,000,000: Total time โ‰ˆ 0 hours โœ“ (too fast, try slower)2. Try speed = 2,500,000: Total time โ‰ˆ 0 hours โœ“ (still too fast)3. Try speed = 1: Total time = 1 + 3 + 2 = 6 hours โœ“ (perfect!)Answer: Minimum speed = 1 km/hโš ๏ธ Key Insight: Waiting Timeโ€ข First n-1 trains: Must wait for integer hours (use ceiling function)โ€ข Last train: Can arrive at any time (use exact division)โ€ข If 1st train takes 1.3 hours, you wait 0.7 hours to board 2nd train at hour 2Time Complexity: O(n log(10^7)) โ‰ˆ O(23n)Much better than brute force O(10^7 ร— n)!
Understanding the Visualization
1
Check Feasibility
If we need more integer hours than our total budget, it's impossible
2
Set Search Range
Search speeds from 1 to 10^7 km/h
3
Binary Search
Test middle speed and narrow down based on whether it works
4
Calculate Travel Time
For each speed: sum of ceil(dist[i]/speed) for first n-1 trains + dist[n-1]/speed
Key Takeaway
๐ŸŽฏ Key Insight: This problem demonstrates the power of binary search on answer spaces. When we can efficiently check if a candidate solution works and the problem has monotonic properties, binary search reduces time complexity from O(S ร— n) to O(log S ร— n).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(S ร— n)

Where S is the maximum possible speed (10^7) and n is number of trains. We might check up to 10^7 speeds, and for each speed we calculate time for n trains.

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables to track current speed and time calculations

n
2n
โœ“ Linear Space

Constraints

  • n == dist.length
  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค dist[i] โ‰ค 105
  • 1 โ‰ค hour โ‰ค 109
  • hour will have at most two digits after the decimal point
  • The answer is guaranteed to not exceed 107
Asked in
Google 12 Amazon 8 Microsoft 6 Apple 4
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen