1883. Minimum Skips to Arrive at Meeting On Time LeetCode Solution
In this guide, you will get 1883. Minimum Skips to Arrive at Meeting On Time LeetCode Solution with the best time and space complexity. The solution to Minimum Skips to Arrive at Meeting On Time problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Skips to Arrive at Meeting On Time solution in C++
- Minimum Skips to Arrive at Meeting On Time solution in Java
- Minimum Skips to Arrive at Meeting On Time solution in Python
- Additional Resources

Problem Statement of Minimum Skips to Arrive at Meeting On Time
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.
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
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n^2)
1883. Minimum Skips to Arrive at Meeting On Time LeetCode Solution in C++
class Solution {
public:
int minSkips(vector<int>& dist, int speed, int hoursBefore) {
constexpr double kInf = 1e7;
constexpr double kEps = 1e-9;
const int n = dist.size();
// dp[i][j] := the minimum time, where i is the number of roads we traversed
// so far and j is the number of skips we did
vector<vector<double>> dp(n + 1, vector<double>(n + 1, kInf));
dp[0][0] = 0;
for (int i = 1; i <= n; ++i) {
const double d = dist[i - 1];
dp[i][0] = ceil(dp[i - 1][0] + d / speed - kEps);
for (int j = 1; j <= i; ++j)
dp[i][j] = min(dp[i - 1][j - 1] + d / speed,
ceil(dp[i - 1][j] + d / speed - kEps));
}
for (int j = 0; j <= n; ++j)
if (dp[n][j] <= hoursBefore)
return j;
return -1;
}
};
/* code provided by PROGIEZ */
1883. Minimum Skips to Arrive at Meeting On Time LeetCode Solution in Java
class Solution {
public int minSkips(int[] dist, int speed, int hoursBefore) {
final double kInf = 1e7;
final double kEps = 1e-9;
final int n = dist.length;
// dp[i][j] := the minimum time, where i is the number of roads we traversed
// so far and j is the number of skips we did
double[][] dp = new double[n + 1][n + 1];
Arrays.stream(dp).forEach(A -> Arrays.fill(A, kInf));
dp[0][0] = 0;
for (int i = 1; i <= n; ++i) {
final double d = dist[i - 1];
dp[i][0] = Math.ceil(dp[i - 1][0] + d / speed - kEps);
for (int j = 1; j <= i; ++j)
dp[i][j] =
Math.min(dp[i - 1][j - 1] + d / speed, Math.ceil(dp[i - 1][j] + d / speed - kEps));
}
for (int j = 0; j <= n; ++j)
if (dp[n][j] <= hoursBefore)
return j;
return -1;
}
}
// code provided by PROGIEZ
1883. Minimum Skips to Arrive at Meeting On Time LeetCode Solution in Python
class Solution:
def minSkips(self, dist: list[int], speed: int, hoursBefore: int) -> int:
kInf = 10**7
kEps = 1e-9
n = len(dist)
# dp[i][j] := the minimum time, where i is the number of roads we traversed
# so far and j is the number of skips we did
dp = [[kInf] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
for i, d in enumerate(dist, 1):
dp[i][0] = math.ceil(dp[i - 1][0] + d / speed - kEps)
for j in range(1, i + 1):
dp[i][j] = min(dp[i - 1][j - 1] + d / speed,
math.ceil(dp[i - 1][j] + d / speed - kEps))
for j, time in enumerate(dp[-1]):
if time <= hoursBefore:
return j
return -1
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.