Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

1444. Number of Ways of Cutting a Pizza

Explanation:

To solve this problem, we can use dynamic programming with memoization. We will create a 3D DP array where dp[i][j][k] represents the number of ways to cut the pizza starting from cell (i, j) into k pieces, such that each piece contains at least one apple.

  1. We will start by calculating the prefix sum of apples from each cell (i, j) to the bottom-right corner of the pizza.
  2. Then, we will iterate from the bottom-right corner towards the top-left corner to consider all possible cuts.
  3. For each cell (i, j), we will iterate over the remaining cuts (k - 1) in either horizontal or vertical direction and check if the cut divides the pizza into two pieces, each containing at least one apple.
  4. We will recursively calculate the number of ways for each cut and update the DP array accordingly.
  5. Finally, the result will be stored in dp[0][0][k].

Time Complexity: O(rows * cols * k^2) Space Complexity: O(rows * cols * k)

:

class Solution {
    private final int MOD = 1000000007;

    public int ways(String[] pizza, int k) {
        int rows = pizza.length;
        int cols = pizza[0].length();

        int[][] prefixApples = new int[rows + 1][cols + 1];
        for (int i = rows - 1; i >= 0; i--) {
            for (int j = cols - 1; j >= 0; j--) {
                prefixApples[i][j] = prefixApples[i + 1][j] + prefixApples[i][j + 1] - prefixApples[i + 1][j + 1] + (pizza[i].charAt(j) == 'A' ? 1 : 0);
            }
        }

        Integer[][][] dp = new Integer[rows][cols][k + 1];

        return dfs(0, 0, k, rows, cols, prefixApples, dp);
    }

    private int dfs(int i, int j, int k, int rows, int cols, int[][] prefixApples, Integer[][][] dp) {
        if (prefixApples[i][j] == 0) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        if (dp[i][j][k] != null) {
            return dp[i][j][k];
        }

        int ways = 0;

        for (int x = i + 1; x < rows; x++) {
            if (prefixApples[i][j] - prefixApples[x][j] > 0) {
                ways = (ways + dfs(x, j, k - 1, rows, cols, prefixApples, dp)) % MOD;
            }
        }

        for (int y = j + 1; y < cols; y++) {
            if (prefixApples[i][j] - prefixApples[i][y] > 0) {
                ways = (ways + dfs(i, y, k - 1, rows, cols, prefixApples, dp)) % MOD;
            }
        }

        dp[i][j][k] = ways;
        return ways;
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.