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] <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of different dishes you can make, each taking a certain amount of time to prepare and giving a certain amount of satisfaction. The brute force approach is to test every single combination of dishes you could possibly choose, and for each combination, try every possible way to arrange them in order to see which yields the highest total satisfaction.
Here's how the algorithm would work step-by-step:
import itertools
def max_satisfaction(satisfaction):
# Generate all possible subsets and their valid permutations to evaluate every cooking order
maximum_satisfaction_found = 0
for subset_size in range(1, len(satisfaction) + 1):
for dish_combination in itertools.combinations(satisfaction, subset_size):
for cooking_sequence in itertools.permutations(dish_combination):
current_satisfaction = 0
for time_index, dish_value in enumerate(cooking_sequence):
current_satisfaction += dish_value * (time_index + 1)
# Keep track of the highest satisfaction score discovered across all tested orders
if current_satisfaction > maximum_satisfaction_found:
maximum_satisfaction_found = current_satisfaction
# Return the maximum satisfaction achieved, or zero if no dishes yield positive results
return maximum_satisfaction_foundTo get the absolute best total score for your dishes, the secret is to prepare the most delicious ones last so they get multiplied by the highest possible time factor. By sorting the dishes by their tastiness and working backward from the end of your cooking schedule, you can easily figure out the exact moment when adding another dish stops helping and starts hurting your total score.
Here's how the algorithm would work step-by-step:
class Solution:
def maxSatisfaction(self, satisfaction_levels: list[int]) -> int:
# Sorting ensures least tasty dishes are considered first for earlier time slots.
satisfaction_levels.sort()
max_total_score = 0
current_running_sum = 0
cumulative_score = 0
# Working backward allows tastiest dishes to receive the highest multipliers.
for dish_satisfaction in reversed(satisfaction_levels):
current_running_sum += dish_satisfaction
# If adding the dish decreases or halts growth, further additions will hurt.
if current_running_sum > 0:
cumulative_score += current_running_sum
max_total_score = max(max_total_score, cumulative_score)
else:
break
return max_total_score| Case | How to Handle |
|---|---|
| Empty array or null input representing no dishes | Return zero immediately since no dishes can be prepared to generate satisfaction. |
| Array containing only negative satisfaction values | Sort the array and check if the total sum of all negative values fails to improve the coefficient, returning zero by picking no dishes. |
| Array containing a single dish with a positive or negative satisfaction value | Return the maximum of zero and the single dish satisfaction because taking it at time one yields its direct value. |
| All elements in the satisfaction array are identical | Sorting leaves identical elements adjacent, and our greedy accumulation correctly evaluates whether including all or a subset maximizes the total sum. |
| Highly skewed distribution with extremely large positive and negative numbers | Sorting the array places the smallest negatives first and largest positives last, ensuring greedy prefix sums maximize the like-time coefficient effectively. |
| Maximum-sized inputs with up to five hundred elements | An O(N log N) sorting approach or O(N) suffix-sum greedy approach scales efficiently well within standard time limits. |
| Duplicate satisfaction values present multiple times | Sorting preserves relative order of duplicates and the greedy accumulation treats them independently as distinct sequential dishes. |
| Inputs with extreme boundary values near integer limits | Standard 32-bit signed integers are sufficient since the maximum possible sum fits well within standard integer ranges without overflow. |