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
Race Strategy: Tire Performance vs Change CostTire A [2,3]2sLap 16sLap 218sLap 3Tire B [3,2]3sLap 16sLap 212sLap 3Change Time: 5 secondsOptimal StrategyUse tire switches to minimizetotal time including penaltiesMinimum Time: 21 secondsBalance exponential degradation vs switch cost
Understanding the Visualization
1
Input
Tires with performance formulas and race parameters
2
Process
Calculate tire degradation vs switch costs
3
Output
Minimum total time with optimal tire strategy
Key Takeaway
🎯 Key Insight: Pre-compute tire performance to avoid exponential recalculation
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