Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Minimum Speed to Arrive on Time

Medium Acceptance 69% Points 30

You are given an array dist where dist[i] represents the distance of the ith train ride, and a floating-point number hour representing the total time available. Each train can only depart at an integer hour. This means after finishing a train ride, you may need to wait until the next integer hour before starting the next train, except for the last train which does not require waiting.

Return the minimum integer speed required to travel all distances within the given hour. If it is not possible to arrive on time, return -1.

Examples
Example 1

Example 1

Input: dist = [1,3,2], hour = 6

Output: 1

Explanation: At speed 1, travel times are 1, 3, and 2 hours. Total time = 6 hours, so speed 1 is sufficient.

Example 2

Example 2

Input: dist = [1,3,2], hour = 2.7

Output: 3

Explanation: At speed 3, travel times are ceil(1/3) = 1, ceil(3/3) = 1, and 2/3 ˜ 0.67. Total time ˜ 2.67 hours, which is within 2.7 hours.

Example 3

Example 3

Input: dist = [1,3,2], hour = 1.9

Output: -1

Explanation: Even at very high speed, the minimum possible time is greater than 1.9 hours due to waiting between trains, so it is impossible.

Constraints
  • 1 = dist.length = 105
  • 1 = dist[i] = 105
  • 1 = hour = 10?
  • hour can have up to two decimal places
Topics
Binary Search
Companies
Samsung
Added 19 Jul 2026 69% acceptance
Ln 1, Col 1Sign in to save your code
1 3 2
6
1