-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lecture 118: Reducing Dishes LeetCode
- Loading branch information
1 parent
3add841
commit a218bf1
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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,23 @@ | ||
// Gives TLE | ||
|
||
class Solution { | ||
public: | ||
int solve(vector<int> &satisfaction, int index, int time) { | ||
if (index == satisfaction.size()) { | ||
return 0; | ||
} | ||
|
||
int include = satisfaction[index] * (time+1) + solve(satisfaction, index+1, time+1); | ||
|
||
int exclude = 0 + solve(satisfaction, index+1, time); | ||
|
||
return max(include, exclude); | ||
} | ||
|
||
int maxSatisfaction(vector<int>& satisfaction) { | ||
|
||
sort(satisfaction.begin(), satisfaction.end()); | ||
|
||
return solve(satisfaction, 0 , 0); | ||
} | ||
}; |
This file contains 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,33 @@ | ||
// Recusrion + Memoization | ||
|
||
class Solution { | ||
public: | ||
int solve(vector<int> &satisfaction, int index, int time, vector<vector<int>> &dp) { | ||
if (index == satisfaction.size()) { | ||
return 0; | ||
} | ||
|
||
if (dp[index][time] != -1) { | ||
return dp[index][time]; | ||
} | ||
|
||
int include = satisfaction[index] * (time+1) + solve(satisfaction, index+1, time+1, dp); | ||
|
||
int exclude = 0 + solve(satisfaction, index+1, time, dp); | ||
|
||
dp[index][time] = max(include, exclude); | ||
|
||
return dp[index][time]; | ||
} | ||
|
||
int maxSatisfaction(vector<int>& satisfaction) { | ||
|
||
int n = satisfaction.size(); | ||
|
||
sort(satisfaction.begin(), satisfaction.end()); | ||
|
||
vector<vector<int>> dp(n+1, vector<int>(n+1, -1)); | ||
|
||
return solve(satisfaction, 0 , 0, dp); | ||
} | ||
}; |
This file contains 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,29 @@ | ||
// Tabulation Mehthod | ||
|
||
class Solution { | ||
public: | ||
int solve(vector<int> &satisfaction) { | ||
int n = satisfaction.size(); | ||
|
||
vector<vector<int>> dp(n+1, vector<int>(n+1, 0)); | ||
|
||
for (int index=n-1; index>=0; index--) { | ||
for (int time=index; time>=0; time--) { | ||
int include = satisfaction[index] * (time+1) + dp[index+1][time+1]; | ||
|
||
int exclude = 0 + dp[index+1][time]; | ||
|
||
dp[index][time] = max(include, exclude); | ||
} | ||
} | ||
|
||
return dp[0][0]; | ||
} | ||
|
||
int maxSatisfaction(vector<int>& satisfaction) { | ||
|
||
sort(satisfaction.begin(), satisfaction.end()); | ||
|
||
return solve(satisfaction); | ||
} | ||
}; |
This file contains 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,31 @@ | ||
// Space Optimization | ||
|
||
class Solution { | ||
public: | ||
int solve(vector<int> &satisfaction) { | ||
int n = satisfaction.size(); | ||
|
||
vector<int> curr(n+1, 0); | ||
vector<int> next(n+1, 0); | ||
|
||
for (int index=n-1; index>=0; index--) { | ||
for (int time=index; time>=0; time--) { | ||
int include = satisfaction[index] * (time+1) + next[time+1]; | ||
|
||
int exclude = 0 + next[time]; | ||
|
||
curr[time] = max(include, exclude); | ||
} | ||
next = curr; | ||
} | ||
|
||
return next[0]; | ||
} | ||
|
||
int maxSatisfaction(vector<int>& satisfaction) { | ||
|
||
sort(satisfaction.begin(), satisfaction.end()); | ||
|
||
return solve(satisfaction); | ||
} | ||
}; |
This file contains 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,24 @@ | ||
// Using Greedy Approach | ||
|
||
class Solution { | ||
public: | ||
int maxSatisfaction(vector<int>& satisfaction) { | ||
sort(satisfaction.begin(), satisfaction.end()); | ||
|
||
int totalSatisfaction = 0, currentSum = 0; | ||
|
||
// Traverse from the last element to the first | ||
for (int i = satisfaction.size() - 1; i >= 0; --i) { | ||
// If adding satisfaction[i] increases the total, add it | ||
if (currentSum + satisfaction[i] > 0) { | ||
currentSum += satisfaction[i]; | ||
totalSatisfaction += currentSum; | ||
} | ||
else { | ||
break; // Stop if further additions would decrease the total | ||
} | ||
} | ||
|
||
return totalSatisfaction; | ||
} | ||
}; |
This file contains 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,5 @@ | ||
# Lecture Codes | ||
|
||
| **Problem** | **Solution** | **Level** | | ||
|:--------------|:--------------:|:-----------:| | ||
| [1402. Reducing Dishes](https://leetcode.com/problems/reducing-dishes/description/) | [Solution1](), [Solution2](), [Solution3](), [Solution4](), [Solution5]() | Hard | |