LeetCode 题解工作台
做菜顺序
一个厨师收集了他 n 道菜的满意程度 satisfaction ,这个厨师做出每道菜的时间都是 1 单位时间。 一道菜的 「 like-time 系数 」定义为烹饪这道菜结束的时间(包含之前每道菜所花费的时间)乘以这道菜的满意程度,也就是 time[i] * satisfaction[i] 。 返回…
4
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
假如我们只选择一道菜,那么我们应该选择满意度最大的那道菜 ,并且判断 是否大于 0,如果 $s_0 \leq 0$,那么我们就不做菜了,否则我们做这道菜,得到的总满意度为 。 假如我们选择两道菜,那么我们应该选择满足度最大的两道菜 和 ,满意度为 $s_1 + 2 \times s_0$,此时要保证选择之后的满意度大于选择之前的满意度,即 $s_1 + 2 \times s_0 > s_0$,…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
一个厨师收集了他 n 道菜的满意程度 satisfaction ,这个厨师做出每道菜的时间都是 1 单位时间。
一道菜的 「 like-time 系数 」定义为烹饪这道菜结束的时间(包含之前每道菜所花费的时间)乘以这道菜的满意程度,也就是 time[i]*satisfaction[i] 。
返回厨师在准备了一定数量的菜肴后可以获得的最大 like-time 系数 总和。
你可以按 任意 顺序安排做菜的顺序,你也可以选择放弃做某些菜来获得更大的总和。
示例 1:
输入:satisfaction = [-1,-8,0,5,-9] 输出:14 解释:去掉第二道和最后一道菜,最大的 like-time 系数和为 (-1*1 + 0*2 + 5*3 = 14) 。每道菜都需要花费 1 单位时间完成。
示例 2:
输入:satisfaction = [4,3,2] 输出:20 解释:可以按照任意顺序做菜 (2*1 + 3*2 + 4*3 = 20)
示例 3:
输入:satisfaction = [-1,-4,-5] 输出:0 解释:大家都不喜欢这些菜,所以不做任何菜就可以获得最大的 like-time 系数。
提示:
n == satisfaction.length1 <= n <= 500-1000 <= satisfaction[i] <= 1000
解题思路
方法一:贪心 + 排序
假如我们只选择一道菜,那么我们应该选择满意度最大的那道菜 ,并且判断 是否大于 0,如果 ,那么我们就不做菜了,否则我们做这道菜,得到的总满意度为 。
假如我们选择两道菜,那么我们应该选择满足度最大的两道菜 和 ,满意度为 ,此时要保证选择之后的满意度大于选择之前的满意度,即 ,即 只要满足 ,我们就可以选择这两道菜。
依此类推,我们可以得到一个规律,即我们应该选择满意度最大的 道菜,并且保证前 道菜的满意度之和大于 。
在实现上,我们可以先对所有菜的满意度进行排序,然后从满意度最大的菜开始选择,每次累加当前这道菜的满意度,如果累加的结果小于等于 ,那么我们就不再选择后面的菜了,否则我们就选择这道菜。
时间复杂度 ,空间复杂度 。其中 是数组的长度。
class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort(reverse=True)
ans = s = 0
for x in satisfaction:
s += x
if s <= 0:
break
ans += s
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity depends on the dynamic programming approach. The solution typically runs in O(n log n) due to sorting and the dynamic programming step, where `n` is the number of dishes. The space complexity is O(n), as we need to store intermediate results for each subset of dishes. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Can the candidate efficiently use dynamic programming with state transitions to reduce computation time?
- question_mark
Does the candidate recognize the importance of sorting dishes in decreasing order of satisfaction for optimizing results?
- question_mark
Is the candidate able to optimize the space complexity while maintaining correctness in the dynamic programming approach?
常见陷阱
外企场景- error
Not considering the importance of sorting dishes, which can lead to suboptimal solutions.
- error
Confusing the order of operations in dynamic programming, such as forgetting to update previous states or incorrectly tracking the maximum sum.
- error
Overcomplicating the space complexity, when it can be reduced by storing only necessary results.
进阶变体
外企场景- arrow_right_alt
What if the satisfaction values are all negative? Can the algorithm still function effectively?
- arrow_right_alt
How would this problem change if there were a maximum limit on how many dishes could be prepared?
- arrow_right_alt
How would the solution scale if the number of dishes is significantly larger than 500?