From a97af5143e1f904001811722f72e64dcb11a4105 Mon Sep 17 00:00:00 2001 From: Matt Chang Date: Fri, 31 Mar 2023 10:46:35 +0800 Subject: [PATCH] ADD # 1444. Number of Ways of Cutting a Pizza --- DP/cutPizza.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ DP/readme.md | 38 ++++++++++++++++++++++++++++ test/dp_test.go | 17 +++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 DP/cutPizza.go diff --git a/DP/cutPizza.go b/DP/cutPizza.go new file mode 100644 index 0000000..d22b0e2 --- /dev/null +++ b/DP/cutPizza.go @@ -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] + +} diff --git a/DP/readme.md b/DP/readme.md index 8bd4dac..4888059 100644 --- a/DP/readme.md +++ b/DP/readme.md @@ -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: diff --git a/test/dp_test.go b/test/dp_test.go index b8a9c6d..82f10b8 100644 --- a/test/dp_test.go +++ b/test/dp_test.go @@ -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