-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1402. Reducing Dishes.cpp
70 lines (63 loc) · 2.03 KB
/
1402. Reducing Dishes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*********** Method-1 (TC-O(N^2), SC-O(1)) *******************
class Solution {
public:
int maxSatisfaction(vector<int>& satisfaction) {
int n = satisfaction.size();
sort(satisfaction.begin(), satisfaction.end());
if(satisfaction[n-1] <= 0) return 0;
int ans = INT_MIN;
for(int i = 0; i < n; i++) {
int counter = 1;
int sum = 0;
for(int j = i; j < n; j++) {
sum += counter*satisfaction[j];
counter++;
}
ans = max(ans, sum);
}
return ans;
}
};
*************************************************************/
/***************** Method-2 (DP, TC-O(NlogN), SC-O(N)) *******************
class Solution {
public:
int maxSatisfaction(vector<int>& satisfaction) {
int n = satisfaction.size();
sort(satisfaction.begin(), satisfaction.end());
if(satisfaction[n-1] <= 0) return 0;
int ans = 0;
vector<int> suffixSum(n, 0);
vector<int> dp(n, 0);
suffixSum[n-1] = satisfaction[n-1];
for(int i = n-2; i >= 0; i--) {
suffixSum[i] = satisfaction[i] + suffixSum[i+1];
}
dp[n-1] = satisfaction[n-1];
ans = max(ans, dp[n-1]);
for(int i = n-2; i >= 0; i--) {
dp[i] = dp[i+1] + suffixSum[i];
ans = max(ans, dp[i]);
}
return max(ans, 0);
}
};
***************************************************************/
/*********** Method-3 (DP, TC-O(NlogN), SC-O(1)) *****************/
class Solution {
public:
int maxSatisfaction(vector<int>& satisfaction) {
int n = satisfaction.size();
sort(satisfaction.begin(), satisfaction.end());
if(satisfaction[n-1] <= 0) return 0;
int ans = 0;
int suffixSum = 0;
int dp = 0;
for(int i = n-1; i >= 0; i--) {
suffixSum += satisfaction[i];
dp += suffixSum;
ans = max(ans, dp);
}
return ans;
}
};