Leetcode 1402. Reducing Dishes

Problem Explanation:

A chef has collected data on the satisfaction level of his n dishes. The 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]. The task is to return the maximum sum of like-time coefficient that the chef can obtain after dishes preparation. Dishes can be prepared in any order and the chef can discard some dishes to achieve this maximum value.

Solution Approach:

The solution approach for this problem is firstly sorting the satisfaction array in descending order. Create two variables ans and sumSatisfaction to store the result and the sum of satisfaction respectively. An iterative loop is set for the satisfaction array. In each iteration, sumSatisfaction is added with each element in the array and if this value is less than or equal to 0, the sum is returned. If not, the value of ans is added with sumSatisfaction. Once the loop has iterated over all elements, we return the ans which has the maximum sum of satisfaction.

Algorithm Walk through:

Take a look at the following example: Let's say the satisfaction level of the dishes is satisfaction = [4,3,2]

  • Sort default: satisfaction = [4,3,2]
  • Iteration = 1, sumSatisfaction = 4, ans = 4
  • Iteration = 2, sumSatisfaction = 7, ans = 11
  • Iteration = 3, sumSatisfaction = 9, ans = 20

The maximum sum of satisfaction the chef can get is 20.

Solution in Python

1
2python
3class Solution:
4    def maxSatisfaction(self, satisfaction: List[int]) -> int:
5        satisfaction.sort(reverse=True)
6        sumSatisfaction = 0
7        ans = 0
8        for s in satisfaction:
9            sumSatisfaction += s
10            if sumSatisfaction <= 0:
11                return ans
12            ans += sumSatisfaction
13        return ans

Solution in JavaScript

1
2javascript
3var maxSatisfaction = function(satisfaction) {
4    satisfaction.sort((a,b)=>b-a);
5    let ans = 0, sumSatisfaction = 0;
6    for(let s of satisfaction){
7        sumSatisfaction += s;
8        if(sumSatisfaction <= 0) return ans;
9        ans += sumSatisfaction;
10    }
11    return ans;
12};

Solution in Java

1
2java
3class Solution {
4    public int maxSatisfaction(int[] satisfaction) {
5        Arrays.sort(satisfaction);
6        int ans = 0, sumSatisfaction = 0;
7        for(int i=satisfaction.length-1;i>=0 && satisfaction[i]+sumSatisfaction>0;i--){
8            sumSatisfaction += satisfaction[i];
9            ans += sumSatisfaction;
10        }
11        return ans;
12    }
13}

Solution in C++

1
2C++
3class Solution {
4public:
5    int maxSatisfaction(vector<int>& satisfaction) {
6        sort(satisfaction.begin(),satisfaction.end(),greater<int>());
7        int ans = 0;
8        int sumSatisfaction = 0;
9        for(int s : satisfaction){
10            sumSatisfaction += s;
11            if(sumSatisfaction <= 0) return ans;
12            ans += sumSatisfaction;
13        }
14        return ans;
15    }
16};

Solution in C#

1
2Csharp
3public class Solution {
4    public int MaxSatisfaction(int[] satisfaction) {
5        Array.Sort(satisfaction);
6        Array.Reverse(satisfaction);
7        int ans = 0,  sumSatisfaction = 0;
8        foreach(int s in satisfaction)
9        {
10            sumSatisfaction += s;
11            if(sumSatisfaction <= 0) return ans;
12            ans += sumSatisfaction;
13        }
14        return ans;
15    }
16}

Conclusion:

The problem of finding the maximum sum of like-time coefficient after preparation of dishes is related to the sorting of arrays and handling of loops. The solutions provided in this article included Python, JavaScript, Java, C++, and C#, which all follow the similar approach of sorting in reverse order, iterating through the array and performing calculations. In algorithmic problems like this, understanding of data structures (like arrays) and control flow (like for loop) is important. Also, understanding of the language-specific syntax and functions (like sort and reverse) is crucial.

In all these solutions, the runtime complexity is determined by the sorting operation, which in most cases is O(n log n), where n is the length of the received array. This makes these solutions fairly efficient, but one should always keep in mind potential trade-offs between efficiency and readability or simplicity of their code.


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫