Problem 393359 · medium · Phase 03 Linear Management & Searching

Fill In the Gaps

binary search on the answer · math

stations is a sorted list of distinct integer positions along a road. You may build k more stations, each at any integer position. Afterwards, look at the distance between every pair of neighbouring stations; return the smallest possible value of the largest such distance.

Examples

Input:  stations = [1, 13], k = 2
Output: 4
Explanation: build at 5 and 9 -> gaps 4, 4, 4.

Input:  stations = [0, 10, 20], k = 1
Output: 10
Explanation: one station cannot shrink both gaps.

Input:  stations = [1, 13], k = 1
Output: 6

Constraints

  • 2 <= len(stations) <= 2 * 10**5, 0 <= k <= 10**6
  • 0 <= stations[i] <= 10**9
  • Required time: O(n log(max(stations))).

Goals

  • Compute how many insertions a gap needs for a given limit with integer arithmetic
  • Minimise a maximum by searching the limit
Starting Python…