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 Time INPUT Road Distances (dist array): 1 km 3 km 4 km i=0 i=1 i=2 Journey Visualization: S E Rest Rest Input Values: dist = [1, 3, 4] speed = 1 km/h hoursBefore = 6 Total dist = 8 km ALGORITHM STEPS 1 Define DP State dp[i][j] = min time to reach road i with j skips 2 Transitions No skip: ceil(time) Skip: keep exact time 3 Build DP Table Fill table for all roads and skip counts DP Table (time in hours): skip 0 1 2 r0 2 1 - r1 6 5 4 r2 10 6 5 4 Find Answer Min skips where time <= hoursBefore FINAL RESULT Optimal Path: Start 1h SKIP 3h R 4h (wait to h5) End Time Breakdown: Road 0: 1h (skip) Road 1: 1+3=4h Wait to: 5h Road 2: 5+1=6h OUTPUT 1 OK - Arrives in 6 hours! With only 1 skip needed Key Insight: DP tracks minimum time to complete each road with varying skip counts. Without rest, time accumulates exactly; with rest, time rounds up to next hour. We find the minimum skips where final time <= hoursBefore. Skipping after road 0 saves 1 hour of waiting, allowing arrival exactly at hour 6! TutorialsPoint - Minimum Skips to Arrive at Meeting On Time | DP Approach
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