A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i] * satisfaction[i].
Return the maximum sum of like-time coefficient that the chef can obtain after preparing some amount of dishes.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
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)
Example 3:
Input: satisfaction = [-1,-4,-5] Output: 0 Explanation: People do not like the dishes. No dish is prepared.
Constraints:
n == satisfaction.length1 <= n <= 500-1000 <= satisfaction[i] <= 1000Problem Overview: You receive an array where satisfaction[i] represents the satisfaction level of cooking a dish. Each dish cooked at time t contributes satisfaction[i] * t to the total like-time coefficient. You can discard dishes and reorder the rest. The goal is to maximize the final score.
Approach 1: Sorting and Selecting Positive Coefficients (Greedy) (Time: O(n log n), Space: O(1))
The key observation: cooking dishes with larger satisfaction later multiplies them by larger time values. Start by sorting the array so negative values appear first and positive values last. Instead of constructing the order forward, iterate from the end and keep a running suffix sum. Each time you add a dish to the front, the contribution increases by the current total satisfaction of all chosen dishes.
If adding a dish makes the cumulative sum negative, further additions will only reduce the result. Stop immediately. This greedy strategy works because adding dishes with positive cumulative impact always increases every future coefficient. Sorting ensures we always test dishes in the most profitable order.
This approach mainly relies on array traversal and sorting. After sorting, a single backward pass determines which prefix of dishes should be kept. The result accumulates using the running sum technique. Because we only track two integers (running sum and result), the extra memory usage stays constant.
Approach 2: Dynamic Programming (Time: O(n^2), Space: O(n^2))
A dynamic programming formulation treats each dish as a decision: cook it or skip it. After sorting the satisfaction array, define dp[i][t] as the maximum score achievable starting from index i when the next cooking time is t. Two transitions exist: skip the dish (dp[i+1][t]) or cook it (satisfaction[i] * t + dp[i+1][t+1]).
The recursion explores every combination of including or excluding dishes while increasing the time multiplier only when a dish is cooked. Memoization or bottom‑up tabulation prevents recomputation. The DP guarantees correctness because it explicitly evaluates all valid subsets and orders after sorting.
This method is useful for understanding the structure of the problem. It makes the optimization decision explicit and mirrors the mathematical definition of the like-time coefficient. However, the quadratic state space makes it slower and heavier than the greedy insight.
Recommended for interviews: The greedy solution is what most interviewers expect. Recognizing that the optimal set of dishes forms a suffix after sorting demonstrates strong problem‑solving intuition. Explaining the DP formulation first can show full understanding, but implementing the greedy approach proves you can reduce the problem to its optimal O(n log n) solution.
To maximize the like-time coefficient, we can sort the satisfaction levels in non-descending order and calculate the possible like-time coefficients by considering prefixes from the end of the sorted array. This allows us to selectively choose higher satisfaction dishes to maximize the total like-time coefficient.
This C program sorts the satisfaction array in decreasing order and iteratively calculates the like-time coefficient. If adding the current satisfaction level to the sum results in a non-positive value, the computation stops as it indicates that further additions would only decrease the total result.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) for using a constant amount of extra space.
An alternative approach involves using dynamic programming to maximize the like-time coefficient. We maintain a DP table where `dp[i]` represents the maximum like-time coefficient attainable using the first `i` dishes. This approach computes overlap coefficients dynamically using state transitions calculated for each dish considered.
This C solution employs dynamic programming by initializing a DP array with zero and iteratively updating it by considering the choice of including a dish or not, thereby maximizing the like-time coefficient.
Time Complexity: O(n^2)
Space Complexity: O(n)
Suppose we only choose one dish, then we should choose the dish with the highest satisfaction s_0, and check whether s_0 is greater than 0. If s_0 leq 0, then we don't cook any dishes, otherwise, we cook this dish, and the total satisfaction is s_0.
If we choose two dishes, then we should choose the two dishes with the highest satisfaction s_0 and s_1, and the satisfaction is s_1 + 2 times s_0. At this time, we need to ensure that the satisfaction after the selection is greater than the satisfaction before the selection, that is, s_1 + 2 times s_0 > s_0, which means as long as s_1 + s_0 > 0, we can choose these two dishes.
By analogy, we can find a rule, that is, we should choose the k dishes with the highest satisfaction, and ensure that the sum of the satisfaction of the first k dishes is greater than 0.
In implementation, we can first sort the satisfaction of all dishes, and then start choosing from the dish with the highest satisfaction. Each time we add the satisfaction of the current dish, if the result of the addition is less than or equal to 0, then we no longer choose the dishes behind, otherwise, we choose this dish.
The time complexity is O(n times log n), and the space complexity is O(log n). Where n is the length of the array.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Sorting and Selecting Positive Coefficients | Time Complexity: O(n log n) due to sorting. |
| Approach 2: Dynamic Programming | Time Complexity: O(n^2) |
| Greedy + Sorting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting + Greedy Suffix Sum | O(n log n) | O(1) | Best practical solution. Use when you recognize the positive cumulative contribution insight. |
| Dynamic Programming | O(n^2) | O(n^2) | Useful for reasoning about include/skip decisions or explaining the optimal structure. |
Reducing Dishes - (MICROSOFT) | Leetcode-1402 | 2 Approaches | Explanation ➕ Live Coding • codestorywithMIK • 9,031 views views
Watch 9 more video solutions →Practice Reducing Dishes with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor