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.
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.
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.
Example 1:
Input: dist = [1,3,2], speed = 4, hoursBefore = 2 Output: 1 Explanation: Without skipping any rests, you will arrive in (1/4 + 3/4) + (3/4 + 1/4) + (2/4) = 2.5 hours. You can skip the first rest to arrive in ((1/4 + 0) + (3/4 + 0)) + (2/4) = 1.5 hours. Note that the second rest is shortened because you finish traveling the second road at an integer hour due to skipping the first rest.
Example 2:
Input: dist = [7,3,5,5], speed = 2, hoursBefore = 10 Output: 2 Explanation: Without skipping any rests, you will arrive in (7/2 + 1/2) + (3/2 + 1/2) + (5/2 + 1/2) + (5/2) = 11.5 hours. You can skip the first and third rest to arrive in ((7/2 + 0) + (3/2 + 0)) + ((5/2 + 0) + (5/2)) = 10 hours.
Example 3:
Input: dist = [7,3,5,5], speed = 1, hoursBefore = 10 Output: -1 Explanation: It is impossible to arrive at the meeting on time even if you skip all the rests.
Constraints:
n == dist.length1 <= n <= 10001 <= dist[i] <= 1051 <= speed <= 1061 <= hoursBefore <= 107Problem Overview: You travel through multiple roads with known distances at a constant speed. After each road, you normally wait until the next integer hour before continuing. You can skip this waiting a limited number of times. The task is to determine the minimum number of skips required to reach the meeting within hoursBefore.
The challenge comes from the rounding rule: if you do not skip, the travel time after each road is rounded up to the next integer hour. This rounding accumulates delays, so choosing the right segments to skip becomes a classic optimization problem.
Approach 1: Dynamic Programming (O(n²) time, O(n²) space)
The standard solution models the problem using dynamic programming. Let dp[i][k] represent the minimum time needed to travel the first i roads using exactly k skips. For each road, you have two choices: do not skip and round the cumulative time up using ceil(), or skip and keep the fractional time. The transition compares these two options and stores the smaller result.
Iterate through all roads and possible skip counts. After filling the table, find the smallest k such that dp[n][k] ≤ hoursBefore. This approach directly simulates the waiting rule and guarantees the optimal solution. It works well because the number of roads is small enough to allow an O(n²) DP state space.
Approach 2: Optimized Dynamic Programming (O(n²) time, O(n) space)
The same DP idea can be implemented with rolling arrays to reduce memory. Instead of storing the full matrix, keep only the previous row while iterating through the roads. Each iteration updates skip states from high to low to avoid overwriting values. The logic stays the same, but memory drops from O(n²) to O(n).
This version is typically preferred in production or interviews when space efficiency matters. The algorithm still iterates through roads and skip counts, performing rounding calculations after each segment.
Approach 3: Greedy with Priority Structure (O(n log n) time, O(n) space)
An alternative perspective focuses on the waiting time introduced by rounding. Every time you round up to the next integer hour, you effectively add a delay equal to the fractional remainder. Skipping a wait removes that delay. The strategy becomes selecting the largest rounding penalties to skip.
While iterating through the roads, track fractional delays and push them into a structure such as a max-heap. When total travel time exceeds the allowed limit, remove the largest delay (simulate skipping that wait). This greedy strategy minimizes the number of skips needed to stay within the deadline.
This method relies on insights about rounding penalties rather than explicitly enumerating DP states. It can be faster in practice but is trickier to derive during interviews.
Recommended for interviews: The dynamic programming solution is what most interviewers expect. It clearly models the decision of skipping versus waiting and demonstrates strong state design. Understanding how rounding affects cumulative travel time is the key insight. The greedy optimization is impressive but less commonly required unless the interviewer pushes for alternative strategies involving arrays and scheduling trade‑offs.
The goal is to calculate the minimum number of skips needed to travel all roads and arrive at the meeting on time. We will use a dynamic programming array dp where dp[i][j] represents the minimum number of skips required to reach road i having already taken j skips. We iterate over each road and simulate the scenarios where you either do or do not skip the waiting time after each.
This solution initializes a 2D dp array. For each road, it considers not skipping and skipping scenarios, updating the dp table accordingly. It calculates the time spent rounding travel time up to the nearest integer when not skipping, and accumulates exact travel time when skipping. The result is extracted by checking which skip count ends up with travel time feasible within hoursBefore.
Python
Java
C#
JavaScript
C
Time Complexity: O(n^2) where n is the number of roads.
Space Complexity: O(n^2) due to the dp table.
An alternative approach would be a greedy strategy mixed with binary search to minimize the skips. However, this is more complex and typically less intuitive than the dynamic programming approach, which explicitly tracks each skip possibility. The feasibility is checked by binary search over possible skips and evaluating if they allow reaching on time.
This C++ solution uses a binary search to find the number of skips needed. It calculates time taken considering the current skip strategy. The search narrows down based on the time it takes compared to allowed hours, attempting different skip counts efficiently.
C++
JavaScript
Time Complexity: O(n log n) due to binary search over skip possibilities multiplied by a linear trip evaluation.
Space Complexity: O(1), since it only uses a few extra variables.
We define f[i][j] as the shortest time considering the first i roads and exactly skipping j rest times. Initially, f[0][0]=0, and the rest f[i][j]=infty.
Since we can choose to skip or not skip the rest time of the i-th road, we can list the state transition equation:
$
f[i][j]=min\left{\begin{aligned} \lceil f[i-1][j]+\frac{d_i}{s}\rceil & Do not skip the rest time of the i-th road \ f[i-1][j-1]+\frac{d_i}{s} & Skip the rest time of the i-th road \end{aligned}\right.
Where \lceil x\rceil represents rounding x up. It should be noted that since we need to ensure that exactly j rest times are skipped, we must have j\le i; moreover, if j=0, no rest time can be skipped.
Due to the possible precision error brought by floating-point operations and rounding up, we introduce a constant eps = 10^{-8} to represent a very small positive real number. We subtract eps before rounding the floating-point number, and finally, when comparing f[n][j] and hoursBefore, we need to add eps.
The time complexity is O(n^2), and the space complexity is O(n^2), where n$ is the number of roads.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n^2) where n is the number of roads. |
| Greedy Approach | Time Complexity: O(n log n) due to binary search over skip possibilities multiplied by a linear trip evaluation. |
| Dynamic Programming | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming (Full Table) | O(n²) | O(n²) | Best for clear state modeling and interview explanations |
| Dynamic Programming (Space Optimized) | O(n²) | O(n) | When memory usage matters but DP logic remains the same |
| Greedy with Heap | O(n log n) | O(n) | When optimizing rounding penalties and minimizing skips dynamically |
LeetCode 1883. Minimum Skips to Arrive at Meeting On Time • Happy Coding • 1,162 views views
Watch 4 more video solutions →Practice Minimum Skips to Arrive at Meeting On Time with our built-in code editor and test cases.
Practice on FleetCode