-
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.
- Loading branch information
1 parent
591cbb1
commit 361feaf
Showing
6 changed files
with
161 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,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); | ||
} | ||
}; |
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,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); | ||
} | ||
}; |
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,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); | ||
} | ||
}; |
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,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); | ||
} | ||
}; |
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** | | ||
|:--------------|:--------------:|:-----------:| | ||
| [1388. Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/description/) | [Solution1](), [Solution2](), [Solution3](), [Solution4]() | Hard | |