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
Visualization
Time & Space Complexity
We try all 2^(n-1) combinations of skip/wait decisions for n-1 roads
Recursion stack depth or space to store current combination
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