Skip to content

Commit

Permalink
Lecture 118: Reducing Dishes LeetCode
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanrajput23 authored Nov 1, 2024
1 parent 3add841 commit a218bf1
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
Binary file added Lectures/Lecture_118/Lecture_118_Hands_On.pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions Lectures/Lecture_118/Lecture_Codes/1402_1.cpp
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);
}
};
33 changes: 33 additions & 0 deletions Lectures/Lecture_118/Lecture_Codes/1402_2.cpp
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);
}
};
29 changes: 29 additions & 0 deletions Lectures/Lecture_118/Lecture_Codes/1402_3.cpp
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);
}
};
31 changes: 31 additions & 0 deletions Lectures/Lecture_118/Lecture_Codes/1402_4.cpp
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);
}
};
24 changes: 24 additions & 0 deletions Lectures/Lecture_118/Lecture_Codes/1402_5.cpp
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;
}
};
5 changes: 5 additions & 0 deletions Lectures/Lecture_118/Lecture_Codes/README.md
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 |

0 comments on commit a218bf1

Please sign in to comment.