Ctrl K

Road Trip With Skipped Breaks

Hard
You are driving to a meeting along a route made of several legs; leg i is legs[i] kilometres long. You drive at a constant speed km/h and set off at time 0. After every leg except the last you must take a break: you wait until the next whole hour before driving on. If a leg ends exactly on a whole hour, there is no wait. For example, finishing a leg at 2.3 hours means the next one starts at 3:00. You may skip some of the breaks and keep driving straight away. The meeting starts deadline hours after you set off. Return the smallest number of skipped breaks that gets you there no later than that, or -1 if it is impossible.
Example 1
legs=[3, 3, 6], speed=4, deadline=3 → 2

Each 3 km leg takes 0.75 hours. With no skips you reach 1:00 after the first leg's break, 2:00 after the second, and finish at 3.5 hours: too late. Skipping only one break still leaves you waiting until 2:00 and arriving at 3.5. Skipping both gives 0.75 + 0.75 + 1.5 = 3 hours exactly. The answer is 2.

Example 2
legs=[5, 5], speed=5, deadline=2 → 0

Each leg takes exactly one hour, so there is no waiting and you arrive on time without skipping anything.

Example 3
legs=[4, 4, 4], speed=2, deadline=5 → -1

Even with no breaks at all the trip takes 6 hours, so it cannot be done.

Constraints
  • 1 ≤ len(legs) ≤ 1000
  • 1 ≤ legs[i] ≤ 10⁵
  • 1 ≤ speed ≤ 10⁶
  • 1 ≤ deadline ≤ 10⁷
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWater Between Walls HardBiggest Solid Panel Block HardProfit From Two Trades HardEvery Way to Space the Words HardCrossing the Cave Hard
loading Python…
Input
legs=[3, 3, 6], speed=4, deadline=3
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.