




Sponsored
Sponsored
To maximize the like-time coefficient, we can sort the satisfaction levels in non-descending order and calculate the possible like-time coefficients by considering prefixes from the end of the sorted array. This allows us to selectively choose higher satisfaction dishes to maximize the total like-time coefficient.
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) for using a constant amount of extra space.
1#include <stdio.h>
2#include <stdlib.h>
3
4int compare(const void* a, const void* b) {
5    return (*(int*)b - *(int*)a);
6}
7
8int maxSatisfaction(int* satisfaction, int size) {
9    qsort(satisfaction, size, sizeof(int), compare);
10    int total = 0, sum = 0;
11    for (int i = 0; i < size; ++i) {
12        sum += satisfaction[i];
13        if (sum > 0) {
14            total += sum;
15        } else {
16            break;
17        }
18    }
19    return total;
20}
21
22int main() {
23    int satisfaction[] = {-1, -8, 0, 5, -9};
24    int size = sizeof(satisfaction) / sizeof(satisfaction[0]);
25    printf("%d", maxSatisfaction(satisfaction, size));
26    return 0;
27}This C program sorts the satisfaction array in decreasing order and iteratively calculates the like-time coefficient. If adding the current satisfaction level to the sum results in a non-positive value, the computation stops as it indicates that further additions would only decrease the total result.
An alternative approach involves using dynamic programming to maximize the like-time coefficient. We maintain a DP table where `dp[i]` represents the maximum like-time coefficient attainable using the first `i` dishes. This approach computes overlap coefficients dynamically using state transitions calculated for each dish considered.
Time Complexity: O(n^2)
Space Complexity: O(n)
1function
This JavaScript approach uses dynamic programming. An array tracks maximum scores achievable and updates state based on including or excluding dishes from past planned courses.