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
Number of Ways of Cutting a Pizza INPUT Pizza Grid (3x3) A . . A A A . . . pizza = ["A..","AAA","..."] k = 3 (pieces needed) cuts = k-1 = 2 = Apple (A) = Empty (.) ALGORITHM STEPS 1 Precompute Apple Counts Count apples in each subgrid 2 Define DP State dp[r][c][k] = ways from (r,c) 3 Try All Valid Cuts Horizontal or Vertical 4 Sum Valid Combinations Ensure each piece has apple 3 Valid Cut Combinations: V then H V then H H then V dp[r][c][k] = sum(dp[nr][nc][k-1]) for valid cuts with apples FINAL RESULT All 3 Valid Cutting Ways: Way 1 V at col 1 H at row 1 Way 2 V at col 1 H at row 2 Way 3 H then V Output: 3 ways to cut [OK] Each piece has at least 1 apple! Key Insight: Use 3D DP with memoization: dp[row][col][cuts] represents number of ways to cut the remaining pizza starting from position (row, col) into 'cuts' pieces. Precompute suffix sums to quickly check if a cut region has at least one apple. Time: O(rows * cols * k * (rows + cols)) | Space: O(rows * cols * k) TutorialsPoint - Number of Ways of Cutting a Pizza | Dynamic Programming Approach
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