Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Greedy
[-1,-8,0,5,-7]
[4,3,2]
[-1,-4,-5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1402: Reducing Dishes
class Solution {
public int maxSatisfaction(int[] satisfaction) {
Arrays.sort(satisfaction);
int ans = 0, s = 0;
for (int i = satisfaction.length - 1; i >= 0; --i) {
s += satisfaction[i];
if (s <= 0) {
break;
}
ans += s;
}
return ans;
}
}
// Accepted solution for LeetCode #1402: Reducing Dishes
func maxSatisfaction(satisfaction []int) (ans int) {
sort.Slice(satisfaction, func(i, j int) bool { return satisfaction[i] > satisfaction[j] })
s := 0
for _, x := range satisfaction {
s += x
if s <= 0 {
break
}
ans += s
}
return
}
# Accepted solution for LeetCode #1402: Reducing Dishes
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
// Accepted solution for LeetCode #1402: Reducing Dishes
struct Solution;
impl Solution {
fn max_satisfaction(mut satisfaction: Vec<i32>) -> i32 {
satisfaction.sort_unstable();
let n = satisfaction.len();
let mut total = 0;
let mut res = 0;
for i in (0..n).rev() {
if satisfaction[i] + total < 0 {
break;
} else {
total += satisfaction[i];
res += total;
}
}
res
}
}
#[test]
fn test() {
let satisfaction = vec![-1, -8, 0, 5, -9];
let res = 14;
assert_eq!(Solution::max_satisfaction(satisfaction), res);
let satisfaction = vec![4, 3, 2];
let res = 20;
assert_eq!(Solution::max_satisfaction(satisfaction), res);
let satisfaction = vec![-1, -4, -5];
let res = 0;
assert_eq!(Solution::max_satisfaction(satisfaction), res);
let satisfaction = vec![-2, 5, -1, 0, 3, -3];
let res = 35;
assert_eq!(Solution::max_satisfaction(satisfaction), res);
}
// Accepted solution for LeetCode #1402: Reducing Dishes
function maxSatisfaction(satisfaction: number[]): number {
satisfaction.sort((a, b) => b - a);
let [ans, s] = [0, 0];
for (const x of satisfaction) {
s += x;
if (s <= 0) {
break;
}
ans += s;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.