↓Skip to main content
  1. LeetCode/

LeetCode 1402: Reducing Dishes

·2 mins· ·
LeetCode Hard Dynamic-Programming Greedy Sorting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-07-08

  • Submissions: Accepted (first try)

Approach
#

Once the dishes are chosen, cooking the most satisfying ones last maximizes the total (rearrangement inequality). So after sorting, the chosen dishes are always the largest few.

Start from the most satisfying and add dishes one at a time. Each new dish goes first and shifts every chosen dish one slot later, multiplying each once more:

new total = old total + (sum of all chosen satisfactions, including the new dish)

As long as the chosen sum is positive, adding increases the total; stop once it is <= 0.

My Solution
#

class Solution:
    def maxSatisfaction(self, satisfaction: List[int]) -> int:
        satisfaction.sort(reverse=True)
        total = suffix = 0                         # suffix: sum of the chosen satisfactions
        for s in satisfaction:
            suffix += s
            if suffix <= 0:
                break
            total += suffix                        # every chosen dish moves one slot later
        return total

My accepted version ran it in reverse: take everything, compute the total, then remove dishes from the smallest up, each removal lowering the total by the remaining sum, keeping the best seen. It is the same idea.

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • Picking only positive dishes: a negative dish placed first makes every later dish count once more, which can be better. The answer 14 for [-1, -8, 0, 5, -9] uses the -1.

Interview Explanation
#

Chosen dishes should be cooked with the most satisfying last, so the chosen set is the largest few. I add from the largest down, putting each new dish first, which shifts the others and adds the current chosen sum to the total; I keep going while that sum is positive. Time O(n log n).

Related

LeetCode 1526: Minimum Number of Increments on Subarrays to Form a Target Array
·1 min
LeetCode Hard Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 1526 note: scanning left to right, every rise above the previous height needs that many new operations; drops reuse earlier operations
LeetCode 1537: Get the Maximum Score
·3 mins
LeetCode Hard Dynamic-Programming Two-Pointers Greedy Array
LeetCode 1537 note: two sorted arrays can switch only at shared values; merge with two pointers, keep a running sum per path, and at a shared value set both to the larger sum plus that value
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 757: Set Intersection Size At Least Two
·2 mins
LeetCode Hard Greedy Sorting
LeetCode 757 note: sort by right end (larger left end first on ties) and track only the two rightmost points; add the two right-end points when uncovered, or the right end when only one is covered
LeetCode 1147: Longest Chunked Palindrome Decomposition
·2 mins
LeetCode Hard Dynamic-Programming Greedy Two-Pointers Rolling-Hash String
LeetCode 1147 note: build strings inward from both ends and cut a pair as soon as they match; cutting early is best, and any leftover middle counts as one chunk
LeetCode 3651: Minimum Cost Path with Teleportations
·2 mins
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Array
LeetCode 3651 note: compute layer by layer over teleports; arriving at a cell by teleport costs the previous layer’s minimum over cells with value >= it, found with a running minimum after sorting, followed by the usual right / down grid DP