LeetCode 题解工作台
完成比赛的最少时间
给你一个下标从 0 开始的二维整数数组 tires ,其中 tires[i] = [f i , r i ] 表示第 i 种轮胎如果连续使用,第 x 圈需要耗时 f i * r i (x-1) 秒。 比方说,如果 f i = 3 且 r i = 2 ,且一直使用这种类型的同一条轮胎,那么该轮胎完成第 …
2
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们注意到,连续使用同一个轮胎 $(f, r)$ 跑 圈,那么第 圈的耗时不应该超过 $changeTime + f$,否则我们可以在第 圈的时候换轮胎,这样总耗时会更少。即: $$
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你一个下标从 0 开始的二维整数数组 tires ,其中 tires[i] = [fi, ri] 表示第 i 种轮胎如果连续使用,第 x 圈需要耗时 fi * ri(x-1) 秒。
- 比方说,如果
fi = 3且ri = 2,且一直使用这种类型的同一条轮胎,那么该轮胎完成第1圈赛道耗时3秒,完成第2圈耗时3 * 2 = 6秒,完成第3圈耗时3 * 22 = 12秒,依次类推。
同时给你一个整数 changeTime 和一个整数 numLaps 。
比赛总共包含 numLaps 圈,你可以选择 任意 一种轮胎开始比赛。每一种轮胎都有 无数条 。每一圈后,你可以选择耗费 changeTime 秒 换成 任意一种轮胎(也可以换成当前种类的新轮胎)。
请你返回完成比赛需要耗费的 最少 时间。
示例 1:
输入:tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4 输出:21 解释: 第 1 圈:使用轮胎 0 ,耗时 2 秒。 第 2 圈:继续使用轮胎 0 ,耗时 2 * 3 = 6 秒。 第 3 圈:耗费 5 秒换一条新的轮胎 0 ,然后耗时 2 秒完成这一圈。 第 4 圈:继续使用轮胎 0 ,耗时 2 * 3 = 6 秒。 总耗时 = 2 + 6 + 5 + 2 + 6 = 21 秒。 完成比赛的最少时间为 21 秒。
示例 2:
输入:tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5 输出:25 解释: 第 1 圈:使用轮胎 1 ,耗时 2 秒。 第 2 圈:继续使用轮胎 1 ,耗时 2 * 2 = 4 秒。 第 3 圈:耗时 6 秒换一条新的轮胎 1 ,然后耗时 2 秒完成这一圈。 第 4 圈:继续使用轮胎 1 ,耗时 2 * 2 = 4 秒。 第 5 圈:耗时 6 秒换成轮胎 0 ,然后耗时 1 秒完成这一圈。 总耗时 = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 秒。 完成比赛的最少时间为 25 秒。
提示:
1 <= tires.length <= 105tires[i].length == 21 <= fi, changeTime <= 1052 <= ri <= 1051 <= numLaps <= 1000
解题思路
方法一:预处理 + 动态规划
我们注意到,连续使用同一个轮胎 跑 圈,那么第 圈的耗时不应该超过 ,否则我们可以在第 圈的时候换轮胎,这样总耗时会更少。即:
我们可以求出满足上式的最大的 ,要使得 最大,那么 和 应该尽可能小,根据题目的数据范围,我们取 , ,那么 ,即 。根据这个结论,以及题目中 的数据范围,我们可以知道 最大为 。
我们定义 表示使用同一个轮胎跑 圈的最小耗时,那么我们可以预处理出 数组,然后使用动态规划求解即可。定义 表示跑 圈的最小耗时,那么我们可以得到状态转移方程:
初始时 ,最终答案为 。
时间复杂度 ,空间复杂度 ,其中 是题目中 , 和 的最大值。本题中 。
class Solution:
def minimumFinishTime(
self, tires: List[List[int]], changeTime: int, numLaps: int
) -> int:
cost = [inf] * 18
for f, r in tires:
i, s, t = 1, 0, f
while t <= changeTime + f:
s += t
cost[i] = min(cost[i], s)
t *= r
i += 1
f = [inf] * (numLaps + 1)
f[0] = -changeTime
for i in range(1, numLaps + 1):
for j in range(1, min(18, i + 1)):
f[i] = min(f[i], f[i - j] + cost[j])
f[i] += changeTime
return f[numLaps]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Look for the ability to optimize state transitions using dynamic programming.
- question_mark
Check if the candidate can handle exponential growth in lap times and tire changes effectively.
- question_mark
Evaluate the candidate's approach to minimizing tire swaps and understanding the impact on time.
常见陷阱
外企场景- error
Forgetting to account for the exponential increase in time for successive laps with each tire.
- error
Improperly handling tire swaps, either by not considering them at all or by switching tires unnecessarily.
- error
Overcomplicating the problem by trying to brute force all possible combinations instead of using dynamic programming.
进阶变体
外企场景- arrow_right_alt
Introduce additional tire types with different increase rates and see how the solution scales.
- arrow_right_alt
Change the structure of the problem by limiting the number of tire swaps or changing the number of laps.
- arrow_right_alt
Modify the problem to include constraints on when tire changes can occur, such as only changing after a set number of laps.