Minimum Skips to Arrive at Meeting On Time - Problem

Imagine you're rushing to an important business meeting and need to travel through n roads to get there. You have exactly hoursBefore hours to make it on time, and you'll be traveling at a constant speed (km/h).

Here's the catch: after completing each road, you must rest and wait until the next integer hour before continuing to the next road. For example, if a road takes 1.4 hours to travel, you must wait until the 2-hour mark before starting the next road. However, if a road takes exactly 2 hours, no waiting is required.

But there's a twist! You can choose to skip some rests to save time. When you skip a rest, you immediately continue to the next road without waiting for the next integer hour.

Your goal is to find the minimum number of skips needed to arrive at your meeting on time, or return -1 if it's impossible even with unlimited skips.

Example: If the first road takes 1.4 hours and the second takes 0.6 hours, skipping the rest after the first road means you'll finish the second road at exactly 2.0 hours, allowing immediate travel on the third road.

Input & Output

example_1.py โ€” Basic Case
$ Input: dist = [1,3,2], speed = 4, hoursBefore = 2
โ€บ Output: 1
๐Ÿ’ก Note: Without skips: 0.25 hours (road 1) โ†’ wait to 1.0 โ†’ 0.75 hours (road 2) โ†’ wait to 2.0 โ†’ 0.5 hours (road 3) โ†’ total 2.5 hours. With 1 skip after road 2: 0.25 โ†’ wait to 1.0 โ†’ 0.75 โ†’ continue immediately โ†’ 0.5 โ†’ total 2.0 hours exactly.
example_2.py โ€” Impossible Case
$ Input: dist = [3,2,4], speed = 1, hoursBefore = 1
โ€บ Output: -1
๐Ÿ’ก Note: Even with maximum skips, the minimum time needed is 9 hours (3+2+4), which exceeds the 1-hour limit. Therefore, it's impossible to arrive on time.
example_3.py โ€” No Skips Needed
$ Input: dist = [1,1,1], speed = 3, hoursBefore = 3
โ€บ Output: 0
๐Ÿ’ก Note: Each road takes 1/3 hour. Total without any skips: 1/3 + wait to 1 + 1/3 + wait to 2 + 1/3 = 2.33 hours, which is within the 3-hour limit.

Visualization

Tap to expand
Journey Optimization: Skip vs WaitStartTime: 0.0Road 1+0.25hWaitโ†’1.0hSkipโ†’0.25hRoad 2+0.75hRoad 2+0.75hFinishโ‰ค2.0h?TravelDecisionPath 1: Wait โ†’ 2.0h (0 skips)Path 2: Skip โ†’ 1.25h (1 skip)Dynamic Programming State Tabledp[road][skips] = minimum_time_to_reach_road_with_exactly_skips_usedโ€ข dp[0][0] = 0 (start with no time, no skips)โ€ข For each road: try both skip and wait transitionsโ€ข Return minimum j where dp[n][j] โ‰ค hoursBefore
Understanding the Visualization
1
Start Journey
Begin at station 0 with 0 time elapsed
2
Travel Each Road
Calculate time for current road segment
3
Decision Point
Choose to wait for next hour or pay to skip
4
Track States
Remember best time for each (road, skips) combination
5
Find Minimum
Return smallest skips that arrive on time
Key Takeaway
๐ŸŽฏ Key Insight: Use DP to track all possible (time, skips) states. At each road, we can either wait (ceiling time) or skip (exact time). The optimal solution finds the minimum skips among all valid paths.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n)

We try all 2^(n-1) combinations of skip/wait decisions for n-1 roads

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion stack depth or space to store current combination

n
2n
โšก Linearithmic Space

Constraints

  • n == dist.length
  • 1 โ‰ค n โ‰ค 1000
  • 1 โ‰ค dist[i] โ‰ค 105
  • 1 โ‰ค speed โ‰ค 106
  • 1 โ‰ค hoursBefore โ‰ค 107
  • Time limit: 2 seconds
  • Memory limit: 256 MB
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
31.2K Views
Medium Frequency
~25 min Avg. Time
856 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