-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy path1402_Reducing_Dishes.java
112 lines (88 loc) · 2.96 KB
/
1402_Reducing_Dishes.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*1402_A chef has collected data on the 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 preparing some amount of dishes.
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
Explanation: After Removing the second and last dish, the maximum total like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14).
Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People do not like the dishes. No dish is prepared.
Constraints:
n == satisfaction.length
1 <= n <= 500
-1000 <= satisfaction[i] <= 1000
*/
import java.util.*;
class Solution {
int[][] dp;
int max=Integer.MIN_VALUE;
public int maxSatisfaction(int[] satisfaction) {
dp=new int[satisfaction.length][satisfaction.length];
Arrays.sort(satisfaction);
/*for(int i=0;i<dp.length;i++){
for(int j=0;j<dp[0].length;j++){
dp[i][j]=-1;
}
}*/
return solveSO(satisfaction);
}
public int solveTab(int[] sat){
int[][] dp=new int[sat.length+1][sat.length+1];
for(int i=0;i<dp.length;i++){
for(int j=0;j<dp.length;j++){
dp[i][j]=0;
}
}
for(int i=sat.length-1;i>=0;i--){
for(int j=0;j<sat.length;j++){
int val=sat[i]*(j+1);
int include=val+dp[i+1][j+1];
int exclude=dp[i+1][j];
dp[i][j]=(int)Math.max(include,exclude);
}
}
return dp[0][0];
}
public int solveSO(int[] sat){
int[] curr=new int[sat.length+1];
int[] prev=new int[sat.length+1];
int max=Integer.MIN_VALUE;
for(int i=0;i<dp.length;i++){
prev[i]=0;curr[i]=0;
}
for(int i=sat.length-1;i>=0;i--){
for(int j=0;j<sat.length;j++){
int val=sat[i]*(j+1);
int include=val+prev[j+1];
int exclude=prev[j];
curr[j]=(int)Math.max(include,exclude);
max=(int)Math.max(curr[j],max);
}
prev=curr;
curr=new int[sat.length+1];
}
return prev[0];
}
public int maxLikeTime(int[] sat,int i,int j){
if(i>=sat.length){
return 0;
}
if(dp[i][j]!=-1){
return dp[i][j];
}
int val=sat[i]*(j+1);
int include=val+maxLikeTime(sat,i+1,j+1);
int exclude=maxLikeTime(sat,i+1,j);
dp[i][j]=(int)Math.max(include,exclude);
max=(int)Math.max(include,exclude);
return dp[i][j];
}
}