Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Minimum Speed to Arrive on Time

Medium Acceptance 69.00% Points 30.00

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.

Hints
Hint 1
N/A
Constraints
  • 1 = dist.length = 105
  • 1 = dist[i] = 105
  • 1 = hour = 10?
  • hour can have up to two decimal places
Companies
Samsung
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions