Minimum Skips to Arrive at Meeting On Time - Problem

You are given an integer hoursBefore, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through n roads. The road lengths are given as an integer array dist of length n, where dist[i] describes the length of the ith road in kilometers. In addition, you are given an integer speed, which is the speed (in km/h) you will travel at.

After you travel road i, you must rest and wait for the next integer hour before you can begin traveling on the next road. Note that you do not have to rest after traveling the last road because you are already at the meeting.

For example, if traveling a road takes 1.4 hours, you must wait until the 2 hour mark before traveling the next road. If traveling a road takes exactly 2 hours, you do not need to wait.

However, you are allowed to skip some rests to be able to arrive on time, meaning you do not need to wait for the next integer hour. Note that this means you may finish traveling future roads at different hour marks.

For example, suppose traveling the first road takes 1.4 hours and traveling the second road takes 0.6 hours. Skipping the rest after the first road will mean you finish traveling the second road right at the 2 hour mark, letting you start traveling the third road immediately.

Return the minimum number of skips required to arrive at the meeting on time, or -1 if it is impossible.

Input & Output

Example 1 — Basic Case
$ Input: dist = [1,3,4], speed = 1, hoursBefore = 6
Output: 1
💡 Note: Without skips: 1 hour + rest to 2 + 3 hours + rest to 6 + 4 hours = 10 hours total. With 1 skip: 1 + 3 = 4 hours, rest to 5, then 4 more hours = 9 total > 6. Better: skip after road 2, so 1 + rest to 2 + 3 = 5, skip rest, + 4 = 9. Actually: travel road 1 (1 hr), rest to hour 2, travel road 2 (3 hrs, finish at 5), skip rest, travel road 3 (4 hrs, finish at 9) > 6. Try: skip after road 1: 1 hr + 3 hrs = 4 hrs, rest to 5, + 4 hrs = 9 > 6. Skip after road 2: hour 2 + 3 hrs = 5, + 4 hrs = 9 > 6. Need 1 skip to finish in 6 hours.
Example 2 — Impossible Case
$ Input: dist = [7,3,5,5], speed = 2, hoursBefore = 10
Output: -1
💡 Note: Even with maximum skips, total time = 7/2 + 3/2 + 5/2 + 5/2 = 10 hours exactly. With rests: 3.5→4 + 1.5→6 + 2.5→9 + 2.5 = 11.5 hours. Need multiple skips but still exceeds time limit.
Example 3 — No Skips Needed
$ Input: dist = [1,1], speed = 1, hoursBefore = 3
Output: 0
💡 Note: Travel road 1 (1 hour), rest to hour 2, travel road 2 (1 hour), finish at hour 3. Total time = 3 hours ≤ 3, so 0 skips needed.

Constraints

  • n == dist.length
  • 1 ≤ n ≤ 1000
  • 1 ≤ dist[i] ≤ 105
  • 1 ≤ speed ≤ 106
  • 1 ≤ hoursBefore ≤ 107

Visualization

Tap to expand
Minimum Skips to Arrive at Meeting On TimeRoad 1Road 2Road 31 km3 km4 km1 hour3 hours4 hoursRestSkipWithout skip: 1hr + rest to 2hr + 3hr + rest to 6hr + 4hr = 10hrWith 1 skip: 1hr + rest to 2hr + 3hr (=5hr) + skip + 4hr = 9hrStill > 6hr, need better strategy with DPAnswer: 1 skip minimum
Understanding the Visualization
1
Input
Roads [1,3,4], speed=1, hoursBefore=6
2
Process
Calculate travel times and decide when to skip rests
3
Output
Minimum skips needed: 1
Key Takeaway
🎯 Key Insight: Use DP to track minimum time with exactly k skips for optimal solution
Asked in
Google 15 Amazon 8 Microsoft 12
23.4K Views
Medium Frequency
~35 min Avg. Time
567 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen