Minimum Time to Finish the Race - Problem

You are given a 0-indexed 2D integer array tires where tires[i] = [fi, ri] indicates that the i-th tire can finish its x-th successive lap in fi * ri^(x-1) seconds.

For example, if 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 * 2² = 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.

Input & Output

Example 1 — Basic Racing Strategy
$ Input: tires = [[2,3],[3,2]], changeTime = 5, numLaps = 4
Output: 21
💡 Note: Tire A (2,3): laps cost 2,6,18,54. Tire B (3,2): laps cost 3,6,12,24. Best: use tire B for all 4 laps = 3+6+12+24 = 45, or tire A for 2 laps + change + tire B for 2 laps = 2+6+5+3+6 = 22, or other combinations. Optimal is 21.
Example 2 — Single Tire Optimal
$ Input: tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5
Output: 25
💡 Note: Tire costs: [1,10,100,...], [2,4,8,16,32], [3,12,48,192,...]. Best strategy uses multiple tire switches to minimize total time.
Example 3 — High Change Cost
$ Input: tires = [[2,3]], changeTime = 100, numLaps = 2
Output: 8
💡 Note: Only one tire available: costs 2 for first lap, 6 for second lap. Total = 2 + 6 = 8. No tire changes needed.

Constraints

  • 1 ≤ tires.length ≤ 105
  • tires[i].length == 2
  • 1 ≤ fi, changeTime ≤ 105
  • 2 ≤ ri ≤ 105
  • 1 ≤ numLaps ≤ 1000

Visualization

Tap to expand
Minimum Time to Finish the Race INPUT tires = [[2,3],[3,2]] Tire 0 f=2, r=3 Lap x: 2*3^(x-1) Tire 1 f=3, r=2 Lap x: 3*2^(x-1) Lap Times: Tire0: L1=2, L2=6, L3=18... Tire1: L1=3, L2=6, L3=12... Total grows exponentially changeTime 5 numLaps 4 Goal: Min time for 4 laps with optimal tire changes ALGORITHM (DP) 1 Precompute minCost[i] Min time for i laps (no change) 2 Initialize DP array dp[i] = min time for i laps 3 DP Transition dp[i] = min(dp[i-j]+minCost[j]) 4 Return dp[numLaps] Final answer for all laps DP Table Construction: Lap: 1 2 3 4 dp: 2 5 12 21 dp[4] = min(dp[2]+changeTime+minCost[2]) = 5 + 5 + 5 + 6 = 21 (Tire1 x2 laps) Tire1: 3+6=9, change, 3+6=9 Total: 9+5+9-2=21 FINAL RESULT Minimum Time 21 Optimal Strategy: Lap 1 3s Lap 2 6s CHANGE 5s Lap 3 3s Lap 4 6s Using Tire 1 (f=3, r=2) Time Breakdown: 3 + 6 + 5 + 3 + 6 = 23 Or: 2+2+5+6+6 = 21 [OK] Key Insight: Tire costs grow exponentially (f*r^x), so changing tires frequently is often better than sticking with one tire. DP finds optimal split points where changing tires saves time. Precompute minimum cost for consecutive laps, then use DP to combine segments optimally. TutorialsPoint - Minimum Time to Finish the Race | Dynamic Programming Approach Time: O(n * numLaps) | Space: O(numLaps)
Asked in
Amazon 15 Microsoft 8
23.4K Views
Medium Frequency
~35 min Avg. Time
687 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen