1489. Rally Point Time

Medium3.5 points estimatedBinary SearchGreedyMaths

A group of hikers is spread along a straight trail. Hiker i stands at kilometre mark positions[i] and can walk in either direction at no more than speeds[i] marks per second, and may also stand still. All hikers set off at the same moment and want to meet at one common point of the trail, which need not be a whole number.

Return the smallest time T, in seconds, after which there is a point that every hiker can have reached. With one hiker the answer is 0. The answer is a real number, so it is accepted if its absolute or relative error is at most 1e-6. Only the time is returned, so there is nothing to tie-break. A binary search on T with an O(n) feasibility check gives O(n log(1/eps)) time and O(1) extra space.

Example 1

Input:
positions = [0,10], speeds = [1,1]
Output:
5
Explanation:

The hikers close the 10-mark gap together at 2 marks per second, so they meet after 5 seconds.

Example 2

Input:
positions = [0,10], speeds = [1,4]
Output:
2
Explanation:

Their speeds add up to 5, so the 10-mark gap closes in 2 seconds.

Example 3

Input:
positions = [8], speeds = [3]
Output:
0.0000000000000000000007888609052210118
Explanation:

A single hiker is already at the meeting point, so the time is 0.

Constraints

1 ≤ n ≤ 1000

0 ≤ positions[i] ≤ 109

1 ≤ speeds[i] ≤ 109

Answers within an absolute or relative error of 1e-6 are accepted.

How this problem is judged

Answers
Numbers are accepted within a tolerance of 1.0E-6: |answer - expected| <= 1.0E-6 x max(1, |expected|).
Tolerance
0.000001
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n log(1/eps))
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…