Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-03-29 19:15:45
1402. Reducing Dishes
给你一个阵列satisfaction表示第i个餐点的满意度,定义满意系数为:满意度[i]*时间,
厨师可以决定是否要做某个菜以及做菜的先后顺序,求出厨师可得的最大满意系数。
Example:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total
like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14).
Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
思路:
1.要取得最佳解要时间和满意度越大越好,所以我们先把所有的满意度排序。
2.反向思考,从满意度最大的餐点开始拿如果当前的sum大于0表示第一个做的菜
为正数可以使解变更佳,关键为每次把res加总到sum时都会把先前拿的值多加
一倍。
Java Code:
作者: Firstshadow (IamCatづミ'_'ミづ)   2023-03-29 19:30:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com