Number of Ways to Cut a Pizza
Given a rectangular pizza represented as a rows x cols matrix containing 'A' (apple) and '.', cut the pizza into k pieces using k-1 cuts. For each cut, you choose the direction: horizontal or vertical, then cut the pizza. The piece given away is either the upper part if horizontal or the left part if vertical. Return the number of ways to cut such that each piece contains at least one apple.

30:00

Number of Ways to Cut a Pizza
hard
Topics
Companies

Given a rectangular pizza represented as a rows x cols matrix containing 'A' (apple) and '.', cut the pizza into k pieces using k-1 cuts. For each cut, you choose the direction: horizontal or vertical, then cut the pizza. The piece given away is either the upper part if horizontal or the left part if vertical. Return the number of ways to cut such that each piece contains at least one apple.

Example 1:
Input: {"pizza":["A..","AAA","..."],"k":3}
Output: 3
Constraints:
  • rows==pizza.length\text{rows} == \text{pizza.length}

  • cols==pizza[i].length\text{cols} == \text{pizza}[i].\text{length}

  • 1rows,cols501 \leq \text{rows}, \text{cols} \leq 50

  • pizza[i][j] is 'A' or '.'.

  • 1k101 \leq k \leq 10

Input
arr ={"pizza":["A..","AAA","..."],"k":3}

Pizza is 3 by 3. Cut it into 3 pieces with 2 cuts, every piece holding at least one apple.

State
pizza
["A..", "AAA", "..."]
rows
3
cols
3
pieces
3
origin
[0, 0]
cuts
2
Variables
rows=3
cols=3
pieces=3
Variables
VariableValue
rows3
cols3
pieces3
DepthFunction Call
Stack empty
0/37