Minimum Time to Finish the Race - Problem
Race Strategy Optimization

Imagine you're a Formula 1 strategist managing tire changes during a race! 🏎️

You have access to different tire types, where each tire type i is described by [fi, ri]:
fi: Base time for the first lap with this tire
ri: Degradation factor (time multiplier for successive laps)

The time for the x-th successive lap on tire type i is: fi × ri(x-1)

Example: If fi = 3, ri = 2, then:
• Lap 1: 3 × 20 = 3 seconds
• Lap 2: 3 × 21 = 6 seconds
• Lap 3: 3 × 22 = 12 seconds

Your Mission: Complete numLaps laps in minimum total time. You can:
• Start with any tire type
• Change to any tire type (including the same one) after any lap
• Each tire change costs changeTime seconds
• You have unlimited supply of each tire type

Goal: Return the minimum time to finish all laps!

Input & Output

example_1.py — Basic Race Strategy
$ Input: tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4
Output: 21
💡 Note: Optimal strategy: Start with tire 0, do 2 laps (2 + 6 = 8), change tire (+5), use tire 0 again for 2 laps (2 + 6 = 8). Total: 8 + 5 + 8 = 21 seconds.
example_2.py — Single Tire Race
$ Input: tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5
Output: 25
💡 Note: Best strategy is to use tire 1 for all 5 laps: 2 + 4 + 8 + 16 + 32 = 62. But changing tires can be better: use tire 1 for 2 laps (2+4=6), change (+6), use tire 1 for 2 laps (2+4=6), change (+6), use tire 1 for 1 lap (2). Total: 6+6+6+6+2 = 26. Actually optimal is different combination giving 25.
example_3.py — Edge Case
$ Input: tires = [[1,1]], changeTime = 10, numLaps = 1
Output: 1
💡 Note: Only one tire available, one lap needed. Time = 1 * 1^0 = 1 second. 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 TRACK - 10 LAPS 🏁T1T1PITT2T22 sec6 sec+5 sec3 sec12 secTire Performance AnalysisTire 1: [f=2, r=3]Lap 1: 2×3⁰ = 2 secLap 2: 2×3¹ = 6 secTire 2: [f=3, r=4]Lap 1: 3×4⁰ = 3 secLap 2: 3×4¹ = 12 secChange Cost+5 secondsper changeDynamic Programming Strategydp[i] = minimum time to complete i lapsFor each lap count, try all tire types and segment lengthsdp[i] = min(dp[i-len] + changeTime + segmentTime[tire][len])🎯 Result: Optimal strategy minimizes total race time!
Understanding the Visualization
1
Analyze Tire Performance
Calculate how each tire performs over consecutive laps, considering exponential degradation
2
Find Sweet Spots
Determine the optimal number of consecutive laps for each tire before changing becomes beneficial
3
Dynamic Programming
Build up optimal strategies for increasing lap counts by trying all possible last segments
4
Optimize Transitions
Balance segment performance against tire change time costs to find the global minimum
Key Takeaway
🎯 Key Insight: Pre-compute optimal single-tire segments and use dynamic programming to combine them efficiently, balancing tire degradation against change costs.
Asked in
Meta 25 Amazon 18 Google 15 Microsoft 12
32.0K Views
Medium Frequency
~25 min Avg. Time
845 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