Problem solution · C++

Pizza with 3n Slices

Pizza with 3n Slices: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Dynamic programming
Source
Kamyu LeetCode Solutions
Length
62 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Pizza with 3n Slices, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 62 lines of C++ from the credited upstream file pizza-with-3n-slices.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 4 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codePizza with 3n Slices · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n^2)// Space: O(n) // [observation]// 1. we can never take two adjacent slices// 2. if we want some set of N / 3 non-adjacent slices, there is always a way to take//// [proof]// - for N = 3, it is obviously true.// - for N' = N + 3,//   - because it's impossible to have only one unwanted slices between all wanted slices.//     if it's true, there will be 3N'/2 unwanted slices rather than 2N' unwanted ones, -><-//   - so we can always find a sequence of two unwanted slices with one wanted slice//     to take firstly, then we can find a way to take the remaining N ones by induction, QED // better optimized spaceclass Solution {public:    int maxSizeSlices(vector<int>& slices) {        return max(maxSizeSlicesLinear(slices, 0, slices.size() - 1),                   maxSizeSlicesLinear(slices, 1, slices.size()));    } private:    int maxSizeSlicesLinear(const vector<int>& slices, int start, int end) {        vector<vector<int>> dp(2, vector<int>(slices.size() / 3 + 1));        for (int i = start; i < end; ++i) {            for (int j = min(((i - start + 1) - 1) / 2 + 1, int(slices.size()) / 3);                 j >= 1;                 --j) {                dp[i % 2][j] = max(dp[(i - 1 + 2) % 2][j],                                   dp[(i - 2 + 2) % 2][j - 1] + slices[i]);            }        }        return dp[(end - 1) % 2][slices.size() / 3];    }}; // Time:  O(n^2)// Space: O(n)class Solution2 {public:    int maxSizeSlices(vector<int>& slices) {        return max(maxSizeSlicesLinear(slices, 0, slices.size() - 1),                   maxSizeSlicesLinear(slices, 1, slices.size()));    } private:    int maxSizeSlicesLinear(const vector<int>& slices, int start, int end) {        vector<vector<int>> dp(3, vector<int>(slices.size() / 3 + 1));        for (int i = start; i < end; ++i) {            for (int j = 1;                 j <= min(((i - start + 1) - 1) / 2 + 1, int(slices.size()) / 3);                 ++j) {                dp[i % 3][j] = max(dp[(i - 1 + 3) % 3][j],                                   dp[(i - 2 + 3) % 3][j - 1] + slices[i]);            }        }        return dp[(end - 1) % 3][slices.size() / 3];    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗