You are given a 0-indexed 2D integer array tires where tires[i] = [fi, ri] indicates that the ith tire can finish its xth successive lap in fi * ri(x-1) seconds.
fi = 3 and ri = 2, then the tire would finish its 1st lap in 3 seconds, its 2nd lap in 3 * 2 = 6 seconds, its 3rd lap in 3 * 22 = 12 seconds, etc.You are also given an integer changeTime and an integer numLaps.
The race consists of numLaps laps and you may start the race with any tire. You have an unlimited supply of each tire and after every lap, you may change to any given tire (including the current tire type) if you wait changeTime seconds.
Return the minimum time to finish the race.
Example 1:
Input: tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4 Output: 21 Explanation: Lap 1: Start with tire 0 and finish the lap in 2 seconds. Lap 2: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds. Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds. Lap 4: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds. Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds. The minimum time to complete the race is 21 seconds.
Example 2:
Input: tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5 Output: 25 Explanation: Lap 1: Start with tire 1 and finish the lap in 2 seconds. Lap 2: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds. Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds. Lap 4: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds. Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second. Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds. The minimum time to complete the race is 25 seconds.
Constraints:
1 <= tires.length <= 105tires[i].length == 21 <= fi, changeTime <= 1052 <= ri <= 1051 <= numLaps <= 1000Problem Overview: You have multiple tire types where each lap gets slower due to degradation. After any lap you can change tires with a fixed changeTime. The goal is to finish numLaps with the minimum total time by deciding when to continue with the same tire or perform a tire change.
Approach 1: Greedy + Brute Force for Small Laps (O(t * n^2) time, O(n) space)
The naive idea is to simulate how long a tire performs if you keep using it for consecutive laps. For each tire with base time f and degradation factor r, lap times grow as f, f*r, f*r^2.... If the next lap becomes slower than starting fresh after a tire change (changeTime + f), continuing is pointless. Precompute cumulative times for every tire while it remains beneficial. Then try building the race lap-by-lap using brute force: for every lap count i, test all possible previous segments of consecutive laps. This approach directly models the decision process but becomes expensive when numLaps grows large.
Approach 2: Dynamic Programming with Precomputation (O(t * k + n * k) time, O(n) space)
The key observation: no tire is useful for many consecutive laps because degradation grows exponentially. For each tire, simulate consecutive laps until the next lap exceeds changeTime + f. Track the best possible time to complete exactly k consecutive laps without a tire change. Because growth is exponential, k stays small (usually ≤ 18).
Once these values are precomputed, the problem becomes classic dynamic programming. Let dp[i] be the minimum time to finish i laps. For every possible consecutive segment length k, update:
dp[i] = min(dp[i], dp[i-k] + best[k] + changeTime)
The extra changeTime represents switching tires before the new segment. Handle the first segment separately so it does not pay the initial change cost. Precomputation limits the inner loop to a small constant, turning a potentially quadratic search into a near linear solution.
This method combines insights from array processing and dynamic programming. Instead of exploring every tire choice at every lap, it compresses all tire behavior into the best possible segment times.
Recommended for interviews: Dynamic Programming with precomputation. Interviewers expect you to notice that tire degradation grows exponentially, which bounds the number of useful consecutive laps. Demonstrating the brute-force idea shows understanding of the decision space, but the optimized DP proves you can reduce the search space and build an efficient solution.
In this approach, we precompute the minimum time required to finish 'k' consecutive laps using each tire without changing and use dynamic programming to find the minimum race time by choosing between continuing with the current tire or changing to another tire.
The function begins by initializing variables for precomputation (max_laps) and dynamic programming (dp array). It iterates through the available tires and calculates the minimum time for each possible number of consecutive laps up to a predefined maximum.
Then, using dynamic programming, it calculates the minimum time required for every possible number of laps, considering both continuing with the same tire and changing tires. The solution for all laps is found at the last position of the dp array.
Time Complexity: O(n * max_laps + numLaps * max_laps), where n is the number of tires and max_laps is the predefined maximum number of laps calculated by a single tire.
Space Complexity: O(numLaps), since we store the minimum time for each lap up to numLaps.
For smaller values of numLaps, a greedy approach combined with brute force might be effective. Check each tire's effectiveness over small contiguous sequences of laps to maximize efficiency. If efficient, use it greedily; otherwise, brute force the sequences.
This C++ solution similarly precomputes consecutive lap times before applying dynamic programming to find the most efficient combination of tire with changes over the race distance, embracing both greedy and brute force characteristics for potential use cases like minimized changeTime.
C++
JavaScript
Time Complexity: O(n * max_laps + numLaps * max_laps).
Space Complexity: O(numLaps).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Dynamic Programming with Precomputation | Time Complexity: O(n * max_laps + numLaps * max_laps), where n is the number of tires and max_laps is the predefined maximum number of laps calculated by a single tire. Space Complexity: O(numLaps), since we store the minimum time for each lap up to numLaps. |
| Greedy + Brute Force for Small Laps | Time Complexity: O(n * max_laps + numLaps * max_laps). Space Complexity: O(numLaps). |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy + Brute Force for Small Laps | O(t * n^2) | O(n) | Useful for understanding the decision process or when numLaps is very small |
| Dynamic Programming with Precomputation | O(t * k + n * k) | O(n) | Optimal approach for large numLaps where tire degradation limits consecutive usage |
Minimum Time to Finish the Race | Leetcode 2188 | Contest 282 | DP | Knapsack problem 🔥🔥 • Coding Decoded • 3,191 views views
Watch 7 more video solutions →Practice Minimum Time to Finish the Race with our built-in code editor and test cases.
Practice on FleetCode