Reducing Dishes - Problem

You're a world-renowned chef who has collected satisfaction ratings for your n signature dishes. Each dish takes exactly 1 unit of time to prepare, and you want to maximize your restaurant's success by strategically selecting and ordering your dishes.

The like-time coefficient of each dish is calculated as: cooking_time × satisfaction_level, where cooking_time includes the time for all previously prepared dishes plus the current dish.

Goal: Return the maximum possible sum of like-time coefficients by choosing which dishes to prepare and in what order.

Key Rules:

  • You can discard any dishes you don't want to prepare
  • You can prepare the selected dishes in any order
  • Each dish takes 1 time unit, so the first dish has time=1, second has time=2, etc.

Example: If you prepare dishes with satisfaction [4, 3, 2] in that order, you get: 1×4 + 2×3 + 3×2 = 16

Input & Output

example_1.py — Basic Case
$ Input: satisfaction = [-1, -8, 0, 5, -9]
Output: 14
💡 Note: After sorting by satisfaction: [5, 0, -1, -8, -9]. We select dishes [5, 0, -1] and cook in that order. Like-time coefficient = 1×5 + 2×0 + 3×(-1) = 5 + 0 - 3 = 2. Wait, let me recalculate: we should cook in reverse order [−1, 0, 5] to get 1×(-1) + 2×0 + 3×5 = -1 + 0 + 15 = 14.
example_2.py — All Positive
$ Input: satisfaction = [4, 3, 2]
Output: 20
💡 Note: All dishes have positive satisfaction, so we cook all of them. Optimal order is [2, 3, 4] to get 1×2 + 2×3 + 3×4 = 2 + 6 + 12 = 20.
example_3.py — All Negative
$ Input: satisfaction = [-1, -4, -5]
Output: 0
💡 Note: All dishes have negative satisfaction. No matter what order or combination we choose, the result will be negative. It's better to cook no dishes at all and get 0.

Constraints

  • 1 ≤ satisfaction.length ≤ 500
  • -1000 ≤ satisfaction[i] ≤ 1000
  • Each dish takes exactly 1 time unit to prepare
  • You can choose to prepare any subset of dishes in any order

Visualization

Tap to expand
🍽️ Chef's Menu OptimizationDishes: Soup(-1), Fish(-8), Bread(0), Steak(5), Dessert(-9)Step 1: Sort by Satisfaction (High to Low)🥩5🍞0🍲-1🐟-8🍨-9Step 2: Greedy Selection🥩 Steak(5): Sum=5 → Add ✓🍞 Bread(0): Sum=5 → Add ✓🍲 Soup(-1): Sum=4 → Add ✓🐟 Fish(-8): Sum=-4 → Stop ✗Step 3: Optimal Menu OrderFinal Menu: [Soup, Bread, Steak]Position 1: 🍲 Soup × 1 = -1Position 2: 🍞 Bread × 2 = 0Position 3: 🥩 Steak × 3 = 15Total Satisfaction: 14💡 Key InsightHigher satisfaction dishes should be served later (higher coefficients)Only include dishes that contribute positively to the experience
Understanding the Visualization
1
Rate Your Dishes
Each dish has a satisfaction level - some customers love, some they hate
2
Plan the Order
Dishes served later have higher impact (multiply by position)
3
Choose Wisely
Only include dishes that improve the overall experience
4
Maximize Impact
Higher satisfaction dishes should come later in the meal
Key Takeaway
🎯 Key Insight: The greedy approach works because we always want higher satisfaction dishes to have higher time coefficients, and we only include dishes that make a positive contribution to the total satisfaction.
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 22
89.2K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen