- Notifications
You must be signed in to change notification settings - Fork370
added a dp question#751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
Tushar2001bbdu wants to merge1 commit intocodemistic:mainChoose a base branch fromTushar2001bbdu:feature_branch
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletionsLeetCode Solutions/1402_Reducing_Dishes.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.