-
Notifications
You must be signed in to change notification settings - Fork 3
/
1444-number-of-ways-of-cutting-a-pizza.py
46 lines (34 loc) · 1.3 KB
/
1444-number-of-ways-of-cutting-a-pizza.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 1444. Number of Ways of Cutting a Pizza
# https://leetcode.com/problems/number-of-ways-of-cutting-a-pizza
from functools import lru_cache
class Solution:
def ways(self, pizza: list[str], K: int) -> int:
m, n, MOD = len(pizza), len(pizza[0]), 10 ** 9 + 7
preSum = [[0] * (n + 1) for _ in range(m + 1)]
for r in range(m - 1, -1, -1):
for c in range(n - 1, -1, -1):
preSum[r][c] = preSum[r][c + 1] + preSum[r + 1][c] - preSum[r + 1][c + 1] + (pizza[r][c] == 'A')
@lru_cache(None)
def dp(k, r, c):
if preSum[r][c] == 0: return 0
if k == 0: return 1
ans = 0
for nr in range(r + 1, m):
if preSum[r][c] - preSum[nr][c] > 0:
ans = (ans + dp(k - 1, nr, c)) % MOD
for nc in range(c + 1, n):
if preSum[r][c] - preSum[r][nc] > 0:
ans = (ans + dp(k - 1, r, nc)) % MOD
return ans
return dp(K - 1, 0, 0)
# ********************#
# TEST #
# ********************#
import unittest
class TestStringMethods(unittest.TestCase):
def test_ways(self):
self.assertEqual(Solution.ways(self, ["A..", "AAA", "..."], 3), 3)
self.assertEqual(Solution.ways(self, ["A..", "AA.", "..."], 3), 1)
self.assertEqual(Solution.ways(self, ["A..", "A..", "..."], 1), 1)
if __name__ == '__main__':
unittest.main()