Number of Ways of Cutting a Pizza - Problem

๐Ÿ• Imagine you're at a pizza party and need to share a rectangular pizza with your friends! The pizza is represented as a rows ร— cols grid where each cell contains either:

  • 'A' - an apple topping ๐ŸŽ
  • '.' - an empty space

Your challenge is to cut the pizza into k pieces using exactly k-1 cuts, where each piece must contain at least one apple.

Cutting Rules:

  • Vertical cuts: Split along column boundaries - left piece goes to current person
  • Horizontal cuts: Split along row boundaries - top piece goes to current person
  • The final remaining piece goes to the last person

Return the number of ways to cut the pizza such that everyone gets at least one apple. Since the answer can be huge, return it modulo 109 + 7.

Input & Output

example_1.py โ€” Python
$ Input: pizza = ["A..","AAA","..."], k = 3
โ€บ Output: 3
๐Ÿ’ก Note: There are 3 ways to cut the pizza: 1) Horizontal cut at row 1, then vertical cut at col 1. 2) Vertical cut at col 1, then horizontal cut at row 1. 3) Horizontal cut at row 1, then vertical cut at col 2. Each way results in 3 pieces where each has at least one apple.
example_2.py โ€” Python
$ Input: pizza = ["A..","AA.","..."], k = 3
โ€บ Output: 1
๐Ÿ’ก Note: There's only 1 way to cut this pizza into 3 pieces with each piece having an apple: first cut vertically at column 1 (giving left piece with 2 apples), then cut the remaining piece horizontally at row 1.
example_3.py โ€” Python
$ Input: pizza = ["A..","A..","..."], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: No cuts needed since k=1. The entire pizza goes to one person and it contains apples, so there's 1 way.

Constraints

  • 1 โ‰ค rows, cols โ‰ค 50
  • rows == pizza.length
  • cols == pizza[i].length
  • 1 โ‰ค k โ‰ค 10
  • pizza[i][j] is either 'A' or '.'
  • Each piece must contain at least one apple

Visualization

Tap to expand
๐Ÿ• Original PizzaPerson 1Person 2Person 3Cut 1 (V)Cut 2 (H)Dynamic Programming State TransitionsState: (0,0,3)Full pizza, 3 peopleState: (0,1,2)Right part, 2 peopleState: (1,1,1)Bottom-right, 1 personEach state represents (row, col, people_remaining) and is memoized
Understanding the Visualization
1
Precompute Apple Counts
Build a prefix sum array to instantly check if any rectangle contains apples
2
Try All Cut Positions
For each state (current rectangle, people left), try every possible horizontal and vertical cut
3
Validate and Recurse
If the cut piece has apples, recursively solve for the remaining rectangle with one less person
4
Memoize Results
Cache results to avoid recomputing the same subproblems
Key Takeaway
๐ŸŽฏ Key Insight: Use prefix sums for O(1) apple validation and memoization to avoid recomputing identical subproblems, reducing exponential complexity to polynomial time.
Asked in
Amazon 15 Google 8 Microsoft 5 Meta 3
38.4K Views
Medium Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen