Skip to content

Commit

Permalink
Lecture 121: Pizza with 3n Slices
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanrajput23 authored Nov 3, 2024
1 parent 591cbb1 commit 361feaf
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
Binary file added Lectures/Lecture_121/Lecture_121_Hands_On.pdf
Binary file not shown.
26 changes: 26 additions & 0 deletions Lectures/Lecture_121/Lecture_Codes/1388_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Recursion [TLE]

class Solution {
public:
int solve(int startIndex, int endIndex, vector<int>& slices, int n) {
if (n == 0 || startIndex > endIndex) {
return 0;
}

int take = slices[startIndex] + solve(startIndex+2, endIndex, slices, n-1);

int notTake = 0 + solve(startIndex+1, endIndex, slices, n);

return max(take, notTake);
}

int maxSizeSlices(vector<int>& slices) {
int k = slices.size();

int case1 = solve(0, k-2, slices, k/3);

int case2 = solve(1, k-1, slices, k/3);

return max(case1, case2);
}
};
34 changes: 34 additions & 0 deletions Lectures/Lecture_121/Lecture_Codes/1388_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Recursion + Memoization

class Solution {
public:
int solve(int startIndex, int endIndex, vector<int>& slices, int n, vector<vector<int>> &dp) {
if (n == 0 || startIndex > endIndex) {
return 0;
}

if (dp[startIndex][n] != -1) {
return dp[startIndex][n];
}

int take = slices[startIndex] + solve(startIndex+2, endIndex, slices, n-1, dp);

int notTake = 0 + solve(startIndex+1, endIndex, slices, n, dp);

dp[startIndex][n] = max(take, notTake);

return dp[startIndex][n];
}

int maxSizeSlices(vector<int>& slices) {
int k = slices.size();

vector<vector<int>> dp1(k+1, vector<int>(k+1, -1));
int case1 = solve(0, k-2, slices, k/3, dp1);

vector<vector<int>> dp2(k+1, vector<int>(k+1, -1));
int case2 = solve(1, k-1, slices, k/3, dp2);

return max(case1, case2);
}
};
43 changes: 43 additions & 0 deletions Lectures/Lecture_121/Lecture_Codes/1388_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Tabulation Method

class Solution {
public:
int solve(vector<int>& slices) {
int k = slices.size();

vector<vector<int>> dp1(k+2, vector<int>(k+2, 0));

for (int index=k-2; index>=0; index--) {
for (int n=1; n<=k/3; n++) {
int take = slices[index] + dp1[index+2][n-1];

int notTake = 0 + dp1[index+1][n];

dp1[index][n] = max(take, notTake);
}
}

int case1 = dp1[0][k/3];

vector<vector<int>> dp2(k+2, vector<int>(k+2, 0));

for (int index=k-1; index>=1; index--) {
for (int n=1; n<=k/3; n++) {
int take = slices[index] + dp2[index+2][n-1];

int notTake = 0 + dp2[index+1][n];

dp2[index][n] = max(take, notTake);
}
}

int case2 = dp2[1][k/3];

return max(case1, case2);
}

int maxSizeSlices(vector<int>& slices) {

return solve(slices);
}
};
53 changes: 53 additions & 0 deletions Lectures/Lecture_121/Lecture_Codes/1388_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Space Optimization

class Solution {
public:
int solve(vector<int>& slices) {
int k = slices.size();

vector<int> prev1(k+2, 0);
vector<int> curr1(k+2, 0);
vector<int> next1(k+2, 0);

for (int index=k-2; index>=0; index--) {
for (int n=1; n<=k/3; n++) {
int take = slices[index] + next1[n-1];

int notTake = 0 + curr1[n];

prev1[n] = max(take, notTake);
}

next1 = curr1;
curr1 = prev1;
}

int case1 = curr1[k/3];

vector<int> prev2(k+2, 0);
vector<int> curr2(k+2, 0);
vector<int> next2(k+2, 0);

for (int index=k-1; index>=1; index--) {
for (int n=1; n<=k/3; n++) {
int take = slices[index] + next2[n-1];

int notTake = 0 + curr2[n];

prev2[n] = max(take, notTake);
}

next2 = curr2;
curr2 = prev2;
}

int case2 = curr2[k/3];

return max(case1, case2);
}

int maxSizeSlices(vector<int>& slices) {

return solve(slices);
}
};
5 changes: 5 additions & 0 deletions Lectures/Lecture_121/Lecture_Codes/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Lecture Codes

| **Problem** | **Solution** | **Level** |
|:--------------|:--------------:|:-----------:|
| [1388. Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/description/) | [Solution1](), [Solution2](), [Solution3](), [Solution4]() | Hard |

0 comments on commit 361feaf

Please sign in to comment.