Number of Ways of Cutting a Pizza Problem
Description
LeetCode Problem 1444.
Given a rectangular pizza represented as a rows x cols matrix containing the following characters: 'A' (an apple) and '.' (empty cell) and given the integer k. You have to cut the pizza into k pieces using k-1 cuts.
For each cut you choose the direction: vertical or horizontal, then you choose a cut position at the cell boundary and cut the pizza into two pieces. If you cut the pizza vertically, give the left part of the pizza to a person. If you cut the pizza horizontally, give the upper part of the pizza to a person. Give the last piece of pizza to the last person.
Return the number of ways of cutting the pizza such that each piece contains at least one apple. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:

Input: pizza = ["A..","AAA","..."], k = 3
Output: 3
Explanation: The figure above shows the three ways to cut the pizza. Note that pieces must contain at least one apple.
Example 2:
Input: pizza = ["A..","AA.","..."], k = 3
Output: 1
Example 3:
Input: pizza = ["A..","A..","..."], k = 1
Output: 1
Constraints:
1 <= rows, cols <= 50rows == pizza.lengthcols == pizza[i].length1 <= k <= 10pizzaconsists of characters'A'and'.'only.
Solution
Approach: Suffix Sums + Dynamic Programming
After any number of cuts, the piece that is left is always the bottom-right part of the pizza: rows i.. and columns j... A horizontal cut gives away the top rows, a vertical cut gives away the left columns. So a state is just the corner (i, j) plus how many pieces we still need to make.
To check quickly whether a part has an apple, let apples[i][j] be the number of apples in the piece with corner (i, j). It is a 2D suffix sum:
apples[i][j] = (pizza[i][j] == 'A') + apples[i+1][j] + apples[i][j+1] - apples[i+1][j+1]
Now let f[i][j] be the number of ways to cut the piece at (i, j) into c pieces that all have an apple.
For
c = 1there is nothing to cut:f[i][j] = 1if the piece has an apple, else0.To go from
c - 1toc, try the first cut:- a horizontal cut before row
x(i < x < m) gives away rowsi..x-1. It is allowed if that strip has an apple, i.e.apples[i][j] > apples[x][j], and then the rest can be cut inf[x][j]ways; - a vertical cut before column
y(j < y < n) works the same way withapples[i][j] > apples[i][y]andf[i][y].
Sum all of those (modulo 109 + 7).
- a horizontal cut before row
Repeat k - 1 times; the answer is f[0][0]. The given-away part has to be checked, not just the remaining one: the last piece is checked by the c = 1 case, and every piece cut off on the way is checked when it is cut.
Complexity: O(k · m · n · (m + n)) time and O(m · n) extra space.
class Solution:
def ways(self, pizza: List[str], k: int) -> int:
mod = 10**9 + 7
m, n = len(pizza), len(pizza[0])
# apples[i][j]: apples in the piece made of rows i.. and columns j.. (the piece left after cuts).
apples = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
apples[i][j] = (pizza[i][j] == "A") + apples[i + 1][j] + apples[i][j + 1] - apples[i + 1][j + 1]
# f[i][j]: ways to cut the piece at (i, j) into c pieces; start with c = 1.
f = [[1 if apples[i][j] > 0 else 0 for j in range(n + 1)] for i in range(m + 1)]
for _ in range(k - 1):
g = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m):
for j in range(n):
total = 0
for x in range(i + 1, m): # horizontal cut: give away rows i..x-1
if apples[i][j] > apples[x][j]:
total += f[x][j]
for y in range(j + 1, n): # vertical cut: give away columns j..y-1
if apples[i][j] > apples[i][y]:
total += f[i][y]
g[i][j] = total % mod
f = g
return f[0][0]