A chef has collected data on the
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.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
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 dishes preparation.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
Approach
Java
import java.util.Arrays;public class ReducingDishes {public static void main(String[] args) {int[] satisfaction = { -1, -8, 0, 5, -9 };System.out.println(maxSatisfaction(satisfaction));}static int maxSatisfaction(int[] satisfaction) {Arrays.sort(satisfaction);int sum = 0;int ans = 0;int n = satisfaction.length;for (int i = n - 1; i >= 0; i--) {sum += satisfaction[i];if (sum > 0)ans += sum;elsebreak;}return ans;}}
C++
#include <bits/stdc++.h>using namespace std;int maxSatisfaction(vector<int> &satisfaction){sort(satisfaction.begin(), satisfaction.end());int sum = 0;int ans = 0;int n = satisfaction.size();for (int i = n - 1; i >= 0; i--){sum += satisfaction[i];if (sum > 0)ans += sum;elsebreak;}return ans;}int main(){vector<int> satisfaction = {-1, -8, 0, 5, -9};cout << maxSatisfaction(satisfaction);return 0;}
No comments:
Post a Comment