Skip to content

ADD # 1444. Number of Ways of Cutting a Pizza #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions DP/cutPizza.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package DP

func CurPizza(pizza []string, k int) int {
return ways(pizza, k)
}

func ways(pizza []string, k int) int {
s := make([][]int, 51)
for i := 0; i < len(s); i++ {
s[i] = make([]int, 51)
}
count := func(r1, c1, r2, c2 int) int {
var sum int
for i := r1; i <= r2; i++ {
sum += s[i][c2+1] - s[i][c1]
}
return sum
}
f := make([][][]int, 51)
for i := 0; i < len(f); i++ {
f[i] = make([][]int, 51)
for j := 0; j < len(f[i]); j++ {
f[i][j] = make([]int, 11)
}
}

mod := int(1e9 + 7)
rows, cols := len(pizza), len(pizza[0])
for i := 0; i < rows; i++ {
s[i][0] = 0
for j := 0; j < cols; j++ {
var tmp int
if pizza[i][j] == 'A' {
tmp = 1
}
s[i][j+1] = s[i][j] + tmp
}
}
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
if count(i, j, rows-1, cols-1) > 0 {
f[i][j][0] = 1
}
}
}

for p := 1; p < k; p++ {
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
f[i][j][p] = 0
for q := j; q < cols; q++ {
if count(i, j, rows-1, q) > 0 {
f[i][j][p] = (f[i][j][p] + f[i][q+1][p-1]) % mod
}
}
for q := i; q < rows; q++ {
if count(i, j, q, cols-1) > 0 {
f[i][j][p] = (f[i][j][p] + f[q+1][j][p-1]) % mod
}
}
}
}
}
return f[0][0][k-1]

}
38 changes: 38 additions & 0 deletions DP/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
# 1444. Number of Ways of Cutting a Pizza

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:

![](https://assets.leetcode.com/uploads/2020/04/23/ways_to_cut_apple_1.png)
```
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 <= 50
* rows == pizza.length
* cols == pizza[i].length
* 1 <= k <= 10
* pizza consists of characters 'A' and '.' only.

# 87. Scramble String

We can scramble a string s to get a string t using the following algorithm:
Expand Down
17 changes: 17 additions & 0 deletions test/dp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import (
"testing"
)

func TestCutPizza(t *testing.T) {
testData := []struct {
pizza []string
k int
expected int
}{
{[]string{"A..", "AAA", "..."}, 3, 3},
{[]string{"A..", "AA.", "..."}, 3, 1},
{[]string{"A..", "A..", "..."}, 1, 1},
}

for _, td := range testData {
result := DP.CurPizza(td.pizza, td.k)
assert.Equal(t, result, td.expected)
}
}

func TestIsScramble(t *testing.T) {
testData := []struct {
s1 string
Expand Down