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.length
1 <= n <= 1000
1 <= dist[i] <= 105
1 <= speed <= 106
1 <= hoursBefore <= 107
This problem asks for the minimum number of skips needed to arrive at a meeting on time, given a list of road distances, travel speed, and the total time available before the meeting. We need to account for mandatory rest periods after each road except the last. Skipping a rest means we don't wait for the next full hour before continuing.
The most efficient approach is dynamic programming. We'll use a 2D array dp
where dp[i][j]
represents the minimum time taken to reach the end of road i
after skipping exactly j
rests.
State Transition:
For each road i
, we have two choices:
dp[i-1][j-1]
) plus the time taken to travel road i
(dist[i]/speed
).dp[i-1][j]
), plus the time taken to travel road i
. We then need to round this time up to the nearest integer because of the rest period.Therefore, the recurrence relation is:
dp[i][j] = min(ceil(dp[i-1][j] + dist[i]/speed), dp[i-1][j-1] + dist[i]/speed)
Base Case:
dp[0][0] = 0
(No time taken to reach the start of the journey).
Initialization:
All other cells in dp
are initialized to infinity.
Final Result:
We iterate through the last row of dp
(dp[n]
, where n
is the number of roads). The smallest dp[n][j]
which is less than or equal to hoursBefore
represents the minimum number of skips required. If no such value exists, it's impossible to reach the meeting on time.
import math
def minSkips(dist, speed, hoursBefore):
n = len(dist)
dp = [[float('inf')] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
eps = 1e-8 # Small value to handle floating-point precision
for i in range(1, n + 1):
for j in range(i + 1):
if j < i:
dp[i][j] = min(dp[i][j], math.ceil(dp[i - 1][j] + dist[i - 1] / speed - eps))
if j > 0:
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + dist[i - 1] / speed)
for j in range(n + 1):
if dp[n][j] <= hoursBefore + eps:
return j
return -1
The code implementations in other languages (Java, C++, Go, TypeScript) follow a similar structure, adapting the syntax and data structures to the specific language. The core dynamic programming logic remains the same.